[comp.lang.c] Array intialization

bareta@ihuxv.ATT.COM (Benyukhis) (06/03/89)

The following declaration is illegal.  Why??????

char *a = "string1";
char *b = "string2";
char *c = "string3";

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

What if the compiler was 3 pass one?  

chris@mimsy.UUCP (Chris Torek) (06/03/89)

In article <3420@ihuxv.ATT.COM> bareta@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@mimsy.umd.edu	Path:	uunet!mimsy!chris

evan@plx.UUCP (Evan Bigall) (06/04/89)

In article <3420@ihuxv.ATT.COM> bareta@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 };

The above declarations will fail if they are placed outside of a function
(static) because statics must be initialized with constants.  The value
of the pointer char *a is not constant.  You can make it constant by 
declaring it char a[] = "string1";  Which allocates 8 char's in memory and
effectivly gives you a constant pointer "a" to them.  Note: you will NOT be
able to change the value of "a" (you WILL be able to change the contents
of the 8 chars it points to). 

The above declarations will fail if they are placed inside a function (auto)
because you can not initialize auto arrays.  

The following:

	static char *g[] = {"string1", "string2", "string3"}; 

Will always compile, but may or may not do what you want.  

Evan

>What if the compiler was 3 pass one?  


-- 
Evan Bigall, Plexus Computers, San Jose, CA (408)943-2283 ...!sun!plx!evan
"I barely have the authority to speak for myself, certainly not anybody else"

gwyn@smoke.BRL.MIL (Doug Gwyn) (06/05/89)

In article <3420@ihuxv.ATT.COM> bareta@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 };

If the declaration has file scope, so that the variable `g' has static
storage duration, then the problem is that initializers must be constants,
not contents of variables.  If the declaration has block scope, then the
problem is that run-time initialization of auto aggregates isn't supported
by your compiler.

curtw@hpcllca.HP.COM (Curt Wohlgemuth) (06/06/89)

gwyn@smoke.BRL.MIL (Doug Gwyn) writes:

> In article <3420@ihuxv.ATT.COM> bareta@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 };
> 
> If the declaration has file scope, so that the variable `g' has static
> storage duration, then the problem is that initializers must be constants,
> not contents of variables.  If the declaration has block scope, then the
> problem is that run-time initialization of auto aggregates isn't supported
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> by your compiler.
  ^^^^^^^^^^^^^^^^

I fail to see how this could be a legal initialization of an auto aggregate.
The ANSI Standard says "All the expressions ... in an initializer list for
an object that has aggregate or union type shall be constant expressions.",
which, as others have pointed out, is not the case here, since 'a', 'b',
and 'c' are not constant expressions.

It matters not whether the declaration for 'g' is inside a block or not.

gwyn@smoke.BRL.MIL (Doug Gwyn) (06/08/89)

In article <7330014@hpcllca.HP.COM> curtw@hpcllca.HP.COM (Curt Wohlgemuth) writes:
>It matters not whether the declaration for 'g' is inside a block or not.

I think you're right; somehow I missed that.
When auto aggregate initialization was added by the pANS,
aggregate initializers were required to be constant expressions.