[comp.std.c] Legal forms of constant expressions in initializers

jsa@edg1.UUCP (J. Stephen Adamczyk) (07/07/89)

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?

Steve Adamczyk
uunet!edg1!jsa
(201) 769-8262

dfp@cbnewsl.ATT.COM (david.f.prosser) (07/10/89)

In article <339@edg1.UUCP> jsa@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...