Path: utzoo!attcan!utgpu!jarvis.csri.toronto.edu!mailrus!uwm.edu!cs.utexas.edu!uunet!oresoft!dan From: dan@oresoft.uu.net (Daniel Elbaum) Newsgroups: comp.std.c Subject: Re: What is a constant expression Message-ID: <1989Oct19.192203.14380@oresoft.uu.net> Date: 19 Oct 89 19:22:03 GMT References: <1219@crdos1.crd.ge.COM> Reply-To: dan@oresoft.uu.net (Daniel Elbaum) Organization: Oregon Software, Portland, OR Lines: 43 In article <1219@crdos1.crd.ge.COM> davidsen@crdos1.UUCP (bill davidsen) writes: : : Trying to get a net program working has brought up an interesting :question as to what is a constant expression. : : #define CNTL(c) (#c[0] & 037) : : This expands as follows: CNTL(M) becomes ("M"[0] & 037). No problem, :the string is a constant, the subscript is a constant, the octal value :is a constant, and the compiler was happy. Except... where the macro was :used to initialize a static value, in which case it was "not a constant :expression." : : What am I missing in the standard? In looking up initializers in the :index, section 3.5.7 (L14) uses the term "constant expression." However :the index doesn't seem to list that term. I looked in the expressions :section and didn't find it. Obviously my assumption that it is an :expression made up entirely of elements whose value is known at compile :time" is incorrect. Referring to the 12/88 dpANS: It's listed under "constant" in the index, of course. :-) Section 3.4 describes basically three kinds of constant expressions that can be used in initializers: arithmetic constants, null pointer constants, and address constants plus or minus integral constants. Arithmetic constant expressions can only contain integral constant operands, so they're out. Address constants point to static storage or to functions, and can contain [], ., ->, &, and *, but you're not allowed to use the values of objects obtained by using them. I guess XJ311 didn't want to bother distinguishing between constant-pointer-to-constant-object and constant-pointer-to- arbitrary-object for compile-time rules. -- Spa link snot the temper tent, a few cannery doubt lowed. ({uunet,tektronix,reed,sun!nosun,osu-cis,psu-cs}!oresoft!(dan)@oresoft.uu.net)