Array intialization
Evan Bigall
evan at plx.UUCP
Sun Jun 4 03:32:44 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 };
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"
More information about the Comp.lang.c
mailing list