Path: csiph.com!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c Subject: Re: error: initializer element is not constant| Date: Tue, 09 May 2017 08:11:48 -0700 Organization: None to speak of Lines: 43 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: mx02.eternal-september.org; posting-host="760eeae9966e2029c529e2d23bf0143c"; logging-data="5703"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19hOuFB2El3f3DuLCikgSTS" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) Cancel-Lock: sha1:ikQppo6YoZv/MAQN9i+rpPMYVqU= sha1:lxB6lyw/HvgSRFlhIEClvBtJsiw= Xref: csiph.com comp.lang.c:109597 jak writes: [...] > Ok ok. Bugs have also been put in the middle. The first time I wrote the > declaration in this way: > > char *Date = "01-01-80"; > char **Phrases[] > { > &Date > } > > and gcc gave me a lot of errors. You have two simple syntax errors. You need an "=" before the initializer, and a semicolon after it. With those changes, it compiles. [...] > But I still do not understand why the compiler does not allow me this: > > char *Date = NULL; > char *Phrases[] > { > "Date: ", > Date, > "\r\n" > }; > > In the end I'm just asking him to allocate space to write in an address > exactly as it happens when I write NULL instead of 'Date'. The initializer for an object defined at file scope must consist only of constant expressions. The expression `Date` yields the value of a variable, which obviously is not constant. (A compiler might be able to determine that the object hasn't been modified since its initialization, but that doesn't make its name a constant expression. Adding "const" wouldn't help either; "const" means read-only, not constant.) -- Keith Thompson (The_Other_Keith) kst-u@mib.org Working, but not speaking, for JetHead Development, Inc. "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister"