Problem initializing a structure

Henry Spencer henry at utzoo.uucp
Sun Dec 18 10:47:01 AEST 1988


In article <154 at cjsa.WA.COM> jeff at cjsa.WA.COM (Jeffery Small) writes:
>static char *menu[] = { "aaa", "bbb", "ccc", 0 };
>
>Now, what I actually want to do is to assign the value to X.item in the 
>initialization statement rather than the assignment statement.  So I make
>the following change to the program:
>
>TEST X = { menu, *menu };
>
>but when I attempt to compile this I get:
>>	"z.c", line 10: illegal initialization
>...why doesn't this initialization work...

In the general case, the '*' operator cannot be executed at compile time,
so it is illegal in compile-time initializers.  In this specific case, a
smart compiler could actually compile it, but it is thought desirable to
define whether something is legal C without reference to how smart the
compiler is.

The best you could do would be to define a separate variable to hold "aaa"
and point to it from both menu and X.  This is admittedly inconvenient, but
anyone wanting to initialize complex interlinked structures at compile time
in C had better get used to inconvenience.
-- 
"God willing, we will return." |     Henry Spencer at U of Toronto Zoology
-Eugene Cernan, the Moon, 1972 | uunet!attcan!utzoo!henry henry at zoo.toronto.edu



More information about the Comp.lang.c mailing list