Array intialization

Chris Torek chris at mimsy.UUCP
Sat Jun 3 14:30:14 AEST 1989


In article <3420 at ihuxv.ATT.COM> bareta at ihuxv.ATT.COM (Benyukhis) writes:
>The following declaration is illegal.  Why??????
>
>char *a = "string1";
>char *b = "string2";
>char *c = "string3";
>
>char *g[] = { a, b, c };

It is illegal because initialisers must be constant expressions, and,
by fiat%, initialised variables are not constant expressions---the
situation is comparable to

	int a = 1, b = 2, c = 3;
	int g[] = { a, b, c };

>What if the compiler was 3 pass one?  

The number of passes in any implementation is irrelevant to the language
definition itself (which is where one must turn for questions of legality).

You are probably confusing this with

	char a[] = "string1", b[] = "string2", c[] = "string3";
	char *g[] = { a, b, c };

which *is* legal because the address of a global or static variable
*is* included in the set of things that make up a `constant expression'.
The latter is the same as

	char *g[] = { &a[0], &b[0], &c[0] };

which is comparable to

	int a = 1, b = 2, c = 3;
	int *g[] = { &a, &b, &c };

-----
% All matters of definition are by fiat :-) .  Actually, the reasoning
  behind this is that for initialised variables, the compiler can emit
  the name of the variable and its value, then forget the latter, but
  the linker must be able to resolve addresses anyway, so allowing
  object addresses as constant expressions does not make the system's
  overall job any harder.
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163)
Domain:	chris at mimsy.umd.edu	Path:	uunet!mimsy!chris



More information about the Comp.lang.c mailing list