Re: Pplease could explain

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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";

[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux