Xref: utzoo comp.std.c:1894 comp.lang.c:23063 Path: utzoo!attcan!utgpu!jarvis.csri.toronto.edu!mailrus!uwm.edu!uakari.primate.wisc.edu!xanth!kremer From: kremer@cs.odu.edu (Lloyd Kremer) Newsgroups: comp.std.c,comp.lang.c Subject: Re: What is a constant expression Summary: Does subscripting render expression non-constant? Message-ID: <10235@xanth.cs.odu.edu> Date: 20 Oct 89 19:18:09 GMT References: <1219@crdos1.crd.ge.COM> Followup-To: comp.lang.c Organization: Old Dominion University, Norfolk, Va. Lines: 48 In article <1219@crdos1.crd.ge.COM> davidsen@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!