Re: An Array of structs

Christiaan Hofman <[email protected]>
Newsgroups gmane.comp.macosx.devel
Message-ID <[email protected]>
On Nov 27, 2009, at 12:35, John Love wrote:

> In my .h file, I have:
> 
> typedef struct YearAmt {
> 	NSString  *year;
> 	int       amount;
> } YearAmt;
> 
> extern NSString *testy;
> extern YearAmt gTest[];
> 
> In the corresponding .m file:
> 
> NSString *testy = @"testy";
> YearAmt gTest[] = {{testy, 10}  /*, + others */};
> 
> I get "Initializer element is not constant. This pertains to the NSString* because if I directly substitute the following, no compile error happens:
> YearAmt gTest[] = {{@"testy", 10}
> 
> What fundamental pertaining to C or C++ am I not getting?
> 
> 
> John Love
> Touch the Future! Teach!

Well, the error says it all: you can only use constants to initialize a global variable. testy is a variable, not a constant, while @"testy" is a constant. If you want to initialize a global variable using a non-constant  variable, you should do that in an initializer like +initialize or +load.

Christiaan
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.