ANSIfication of #define ctl(X) ('X' & 0x1f)

Doug Salot doug at feedme.UUCP
Sat Jun 11 10:50:12 AEST 1988


No doubt this has been hashed out before (if so, email from the
solution poster would be appreciated), but what is the accepted
method of porting macro substitution within character constants
to dpANS-conforming compilers?

A method which immediately suggests itself (to a warped mind) for
the control character macro in the subject heading is

	#define ctl(X) (#X [0] & 0x1f)

which does the trick, but has (at least) two problems.  First,
and less worrisome for q&d ports, is that a couple of bytes are
added to the string table for each invocation.  Worse is the
sad fact that most compilers treat "xyz"[0] as a non-constant
runtime expression and disallow the macro expansion in case
statements (where I happen to need it).

Question to satisfy curiosity: is it obvious that a constant string
indexed by a constant isn't a constant?  Clearly, this is an exceptional
case of string indexing, but it should be safe to evaluate the expression
to a constant character at compile-time, shouldn't it?

More importantly, is there a way to do what I want to do without
changing the macro invocations?
-- 
Doug Salot || doug at feedme.UUCP || {trwrb,hplabs}!felix!dhw68k!feedme!doug
Feedme Microsystems:Inventors of the Snarf->Grok->Munge Development Cycle



More information about the Comp.lang.c mailing list