[comp.lang.c] Static initializers

evan@u1100s.UUCP (Evan J. Bigall) (08/21/88)

> char *t = "Hello World" ;
> struct bar {
>   char *z ;
>   int y ;
> } x = { t , 10 } ;   /* t is not allowed here*/
>
> main () {
>   printf ("x.z = %s\n",x.z) ;
> }
>
> ok you C-nuts. Question for you. Why do my compilers complain about the 
> initialization of x.z with t? ("initializer for static variable is not 
> constant" -- gcc) It seems pretty constant to me.

No, its not constant.  You are intializing it with t and t is a char* variable.
t also happens to be initialized but, t is not equivalent to "Hello World"

It makes more sense if you think about it from a more general point of view
where the value of t could change between the initialization of it and that 
of x.  Something like:

	main()
	  { char *t = "Hello World";
	
	    /* lots of code, potentially mucking the value of t */ 

	    { /* begin a sub block */

	       struct bar
                  { char *z;
                    int y;  } x = { t , 10 } ; /* who knows what t is here? */

	     }
	   }

Your code will work if you change the initialization to:

	 struct bar {
	   char *z ;
	   int y ;
	 } x = { "Hello World", 10 } ;   /* t is not allowed here*/        


Does this help?
Evan

brister@td2cad.intel.com (James Brister) (08/24/88)

Thanks to all the people who responded by mail to this question (which I now 
feel _pretty_ embarrased about having asked, but there's only one other person 
around here who knows C so you guy are all I've got (sob sob) :-). I replied to 
you all, but some replies got bounced (#^&%$ mailer!!!).

James "Can you play Twister?" Brister
brister@td2cad.intel.com