I have a 2-dimensional array of pointer to char and initialising it in a header file.
The problem is this: it doesn’t complain getting assigned a const char[] but does not like me assigning const char* (as shown in the code). It gives me an error “initializer element is not constant”.
const char lang[8] = "English";
const char * langPtr = "English 1";
const char * optionPtr[3][10] = {
{lang, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
{langPtr, 0, ...},
{...}
};
I thought lang and langPtr are both pointing at the beginning of a string so should be able to do this. I want to use a pointer to initialise the 2D array. Is there anyway of doing this globally?
In C, elements in initialisers for static objects must be “constant expressions” (all global objects are static).
The address of a static object is an “address constant”, which is a kind of “constant expression” – that’s why
langworks. The value of a variable – even aconstvariable (though note thatlangPtritself is notconst) – is not a “constant expression”, which is whylangPtrdoes not work.Note that this is different in C++, where
constqualified variables are genuine constants.