This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Pplease could explain


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 Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]