Legal forms of constant expressions in initializers

david.f.prosser dfp at cbnewsl.ATT.COM
Mon Jul 10 23:41:34 AEST 1989


In article <339 at edg1.UUCP> jsa at edg1.UUCP (J. Stephen Adamczyk) writes:
>By my reading of section 3.4, both of the following examples are
>not legal:
>
>/* 1 */
>static int i = "abc"[2];
>
>/* 2 */
>static int a[5];
>static int j = &a[3] - &a[2];
>
>The second example, in particular, is interesting, since by my reading
>K&R (first edition) allows it, and the pcc-based compiler on my Sun does
>too.
>
>Anyone feel these are legal?  Have I read the text wrong?

I agree, they cannot be used by strictly conforming programs.

The second one can probably be justified by K&R1, but it is unusual,
at least in my experience, for a C compiler to have accepted such a
construct.  The pcc compilers available to me all did not allow the
initialization.  They don't seem to realize that the address of the
same static duration object was in common.

I know I can come up with other actually constant expressions that
simply would not be accepted as such by almost any C compiler.  It
was this reality that guided the constant expression description in
the draft standard.  At least what's valid and what's invalid has
been made clear.

My biggest complaint about constant expressions in ANSI C is that
constructs such as in the following

	static int *p = (int *)0;

are not valid, even though an initializer of ``0'' or ``(void *)0''
is valid!

Dave Prosser	...not an official X3J11 answer...



More information about the Comp.std.c mailing list