public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* Pplease could explain
@ 2005-03-26 19:05 Andrea Baldoni
  2005-03-27 20:29 ` Kevin P. Fleming
  0 siblings, 1 reply; 2+ messages in thread
From: Andrea Baldoni @ 2005-03-26 19:05 UTC (permalink / raw)
  To: gcc-help

Good morning.
I'm having troubles with "initializer element is not constant".
There is a way to make the compiler knows the second construct (*b) is
also constant?

Please, CC to my email address.

typedef struct foo {
	const unsigned char *bar;
} foo;

int main()
{
	static const unsigned char a[]="aaa";
	static const unsigned char *b="bbb";

	static const foo f[]={
		{ a, },
		{ b, },
	};

}

Best regards,
 Andrea Baldoni

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: Pplease could explain
  2005-03-26 19:05 Pplease could explain Andrea Baldoni
@ 2005-03-27 20:29 ` Kevin P. Fleming
  0 siblings, 0 replies; 2+ messages in thread
From: Kevin P. Fleming @ 2005-03-27 20:29 UTC (permalink / raw)
  To: Andrea Baldoni; +Cc: gcc-help

Andrea Baldoni wrote:
> Good morning.
> I'm having troubles with "initializer element is not constant".
> There is a way to make the compiler knows the second construct (*b) is
> also constant?

Yes, but you didn't.

> int main()
> {
> 	static const unsigned char a[]="aaa";
> 	static const unsigned char *b="bbb";
> 
> 	static const foo f[]={
> 		{ a, },
> 		{ b, },
> 	};
> 
> }

'a' is constant because the compiler can deduce the address of the 
beginning of the array, and you cannot change. That means the value of 
'a' cannot change when the program runs.

The same is not true for b: b is a 'const char *', meaning it is a 
pointer to char and the char it points to cannot be written into. 
However, b _itself_ can be written into, thus references to b are not 
'constant'.

You should be able to fix this by using:

static const unsigned char * const b = "bbb";

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2005-03-27 15:12 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-03-26 19:05 Pplease could explain Andrea Baldoni
2005-03-27 20:29 ` Kevin P. Fleming

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).