gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

C/C++ > ERROR: initializer element is not constant

#48370 - sabkaraja - Tue Jul 19, 2005 4:40 pm

Hi

I have some arrays like the following in a .h file:
extern const u8 a1[512];
extern const u8 a2[512];
extern const u8 a3[512];
extern const u8 a4[512];
and the data defined elsewhere in another file.

I am using the list as follows:

const u8 *MyList[]={
(u8*) a1,
(u8*) a2,
(u8*) a3,
(u8*) a4,
};

I have to populate a struct MyStruct[] (which has a member const u8* someData) based on user selection.

But when I compile the code, it gives me an error saying that:
initializer element for `MyStruct[0].someData' is not constant

I saw one similar post by another member which relates to this, but my problem isnt solved still.

#48377 - poslundc - Tue Jul 19, 2005 6:12 pm

You are casting away the const-ness of your four u8 pointers in your declaration of MyList. The way you have set it up, you probably should not have to typecast at all, but if you do, the type you'd cast to would be (const u8 *), not (u8 *).

Dan.

#48378 - sajiimori - Tue Jul 19, 2005 6:17 pm

You're asking about an error initializing an array of MyStructs, but you didn't post the struct declaration, the array declaration, or the initializers.

#48400 - sabkaraja - Tue Jul 19, 2005 8:27 pm

Sorry about that:

typedef struct StrData_tag
{
const u8* someData;
u16 param0; u16 param1;
}StrData;

The data was assigned into the struct like this:

StrData MyStruct[] = {
{ &MyList[0], 2,7},
{ &MyList[1], 5,2},
{ &MyList[2], 61,8},
{ &MyList[3], 6,4},
};

Above code doesnt seem to work, where as this works
StrData MyStruct[] = {
{ a1, 2,7},
{ a2, 5,2},
{ a3, 61,8},
{ a4, 6,4},
};

TIA

#48406 - poslundc - Tue Jul 19, 2005 9:36 pm

You are casting away the const-ness of your four u8 pointers in your declaration of MyList. The way you have set it up, you probably should not have to typecast at all, but if you do, the type you'd cast to would be (const u8 *), not (u8 *).

Dan (conspicuously similar to my last post).

#48411 - sajiimori - Tue Jul 19, 2005 9:49 pm

I disagree. The casts are not an error because the newly casted non-const pointers will automatically be demoted back to const.

Initializing someData with &MyList[0] is an error because it is a pointer to a pointer. (Recall that MyList is an array of pointers, and you are taking the address of one of those pointers.) It should actually give a different error message than the one you initialially posted, presumably something about not being able to convert u8** to u8*.

On the other hand, if you initialize it with MyList[0] then it should give the error that you posted because MyList[0] is not constant. MyList is an array of non-const pointers to const data. You probably want an array of const pointers to const data, like this:
Code:
const u8* const MyList[] = { ... };

#48416 - poslundc - Tue Jul 19, 2005 10:10 pm

Ah, you're right, I had the const-correctness legality backwards.

Dan.

#48419 - sajiimori - Tue Jul 19, 2005 10:22 pm

You know, the solution I posted (about const pointers to const data) probably won't work, either. Array elements are never compile-time constants, even for const arrays. The addresses of the array elements are known at compile time though, so you could always change someData to u8**.

I've long wished for more things to be compile-time constants in C, including the results of pure functions.

#48455 - sabkaraja - Wed Jul 20, 2005 4:55 am

I was trying to access the MyList data like MyList[gLang]

But when I used MyList[1] instead of gLang, it doesnt give an error. So it confirms what sajiimori said.

But for some reasons, I cannot change the someData. The data structure cannot be touched at all.

I believe there must be some other solution to this?

Thank you Friends

#48505 - tepples - Wed Jul 20, 2005 1:56 pm

sajiimori wrote:
I've long wished for more things to be compile-time constants in C, including the results of pure functions.

Perhaps GCC supports this as an extension to the C language. Have you looked into the pure attribute?
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#48538 - sajiimori - Wed Jul 20, 2005 5:55 pm

Sadly, I don't use GCC at work.