Hi,
I hope someone can help. Code with comments explaining the problem is shown below.
char const char_set1[42] = {'A','B','C','D','E','F','G','H', 'I','J','K','L','M','N','O','P', 'Q','R','S','T','U','V','W','X', 'Y','Z','.','-','%','/',':','0', '1','2','3','4','5','6','7','8','9',' '}; [I then wish to declare a constant pointer to the array] [The following declaration compiles:] char const* char_set_arrayA = char_set1; [But the following declaration fails:] char const* char_set_arrayB[]={char_set1};
Eventually I need to pass a series of character set arrays into this declaration but I cannot get it to compile with one array element (char_set1).
I would be grateful for any advice as to why
char const* char_set_arrayB[]={char_set1};
is not accepted by the compiler. The compiler reports
"error #28: expression must have a constant value"
Is the const declaration incorrect?
Thanks
John McLane
It compiles for me. And I can see no reason why it wouldn't compile. What compiler are you using?
- mike
You probably want the array of pointers to be constant too, but that shouldn't cause a compiler error. Can you try to compile the very minimal source?
char const chars[] = { 'a', 'b' }; char const* const ptrs[] = { chars };
I mean just the two lines, no includes or anything. See if the compiler complains. Mine doesn't, and it's v3.1.0.939 (same as yours.) Maybe you have a macro somewhere that distorts your code in an unexpected way?
Thanks Mike. That's interesting.
I compiled your suggested code in a single file. It compiles fine with just the two lines. No errors.
However, the moment I put it in a simple void function as follows it fails with the same #28 error appears.
void test(void) {
}
?
However, the moment I put it in a simple void function as follows it fails
This one's easy. If chars is an automatic variable, its address is not constant any more and cannot be used as an array initializer. Make it static, and it will work again. It makes sense. An automatic variable is usually placed on the stack, so you cannot know its address before the function is called, so the address is not constant. I'm sure the language standard covers this, but it would take too much time to look it up...
You shouldn't consume stack - or even RAM - for a const table.
View all questions in Keil forum