What is a constant expression
Lloyd Kremer
kremer at cs.odu.edu
Sat Oct 21 05:18:09 AEST 1989
In article <1219 at crdos1.crd.ge.COM> davidsen at crdos1.crd.ge.COM (Wm E Davidsen Jr) writes:
> #define CNTL(c) ('c' & 037)
>While this work fine with pcc, the ANSI preprocessor doesn't expand it.
>
> #define CNTL(c) (#c[0] & 037)
>
> This expands as follows: CNTL(M) becomes ("M"[0] & 037).
>where the macro was
>used to initialize a static value, in which case it was "not a constant
>expression."
>
>Obviously my assumption that it is an
>expression made up entirely of elements whose value is known at compile
>time" is incorrect.
This seemed like an interesting question, and I've been waiting for a response
from one of the real "legal eagles" of the Standard. But since none is
forthcoming, I'll throw in my two cents.
The macro contains an array subscripting expression, and the compiler
apparently fears that the contents of the array may be changed at run time
rendering the expression non-constant. However, notice that the array in
question is a *string literal*. What is this compiler's policy regarding
the writability of string literals?
If they are writable, then the compiler's rejection of the expression is
justified. If they are not, then the compiler is simply seeing the
subscripting operation, and prematurely giving up hope of constancy.
Of course, neither possibility offers a solution. The compiler won't accept
to expression as constant.
I don't suppose it would do any good to put a "const" into the macro.
Just hypothesizing. :-)
P.S. I support the adoption of the Standard, but it is disheartening to keep
seeing its introduction cause working code to break. Growing pains, I guess.
--
Lloyd Kremer
...!uunet!xanth!kremer
Have terminal...will hack!
More information about the Comp.lang.c
mailing list