This thread has been locked.
If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.
In h file:
typedef struct mainScreen_t { uint8_t line; uint8_t position; char value[16]; }mainScreen_t; const mainScreen_t Screen1 = {0, 10, {"GPS"}}; const mainScreen_t Screen2 = {1, 10, {"A/D"}};
In c file:
mainScreen_t lvl_1[LCD_MAIN_MENUS] = {Screen1, Screen2};
CCS error:
"../main.c", line 41: error #28: expression must have a constant value "../main.c", line 41: error #28: expression must have a constant value
I will have a bunch of pre-defined menu's (fixed location with constant text). I want to create an array of them as you can see. I get the following compile error....What do I have to change to not get this error?
Thanks
I don't think you got the point. When I compiled your code with 2 (a guess) in place of that unknown symbol, I got no errors. Which suggests that this symbol is the problem. (Unless GCC is more forgiving than whatever compiler you are using.) What is its definition? Is it a constant expression?
#define LCD_MAIN_MENUS 2 #define EMPTY 0x0F typedef struct mainScreen_t { uint8_t line; uint8_t position; char value[16]; }mainScreen_t; const mainScreen_t Screen1 = {0, 10, {"GPS"}}; const mainScreen_t Screen2 = {1, 10, {"A/D"}};
It is a #define see above. Not sure bcz I replace with a 2 and still get the same error??....I'm using CCS 12.1.0...Compiling using C99
Hi Steven,
I tried your code in my PC. looks normal, no any error:
Thanks!
Best Regards
Johnson
"const" declares a runtime constant, not a compile-time constant. It (conceptually, and maybe actually) puts the variable in memory. So in
> mainScreen_t lvl_1[LCD_MAIN_MENUS] = {Screen1, Screen2};
You're trying to put two variables which are already in memory into a (different) array in memory, but you can't since "Screen1" and "Screen2" are already somewhere else. One thing you could do is:
> mainScreen_t *lvl_1[LCD_MAIN_MENUS] = {&Screen1, &Screen2};
though that will require a dereference each time you use one of them ("lvl_1[0]->position").
Hello Johnson....
Not yet....I am tied up with other customers at the moment....I hope to try Bruce's recommendation in the near future...
Thanks and sorry for the delay
Steve
Hi Steve,
Understood. Waiting for your test result.
Thanks!
Best Regards
Johnson
Tried the line Bruce suggested....
mainScreen_t *lvl_1[LCD_MAIN_MENUS] = {&Screen1, &Screen2};
It produces the following:
#145-D a value of type "const mainScreen_t *" cannot be used to initialize an entity of type "mainScreen_t *"
The message seems to recommend (I missed that before) that you use instead:
> const mainScreen_t *lvl_1[LCD_MAIN_MENUS] = {&Screen1, &Screen2};
Watch the shells, though -- the array "lvl_1" is not declared "const", it merely contains pointers to "const" entities. If you wanted lvl_1 itself to be const you would use:
> const mainScreen_t * const lvl_1[LCD_MAIN_MENUS] = {&Screen1, &Screen2};
which looks a bit odd but can be read as: 'lvl_1 is a const array. If you reference one of its elements you get a pointer to a "const mainScreen_t".'
Bruce...
As you suggested the following works:
const mainScreen_t *lvl_1[LCD_MAIN_MENUS] = {&Screen1, &Screen2};
How is this read?
lvl_1 is a pointer of type const mainScreen_t ?
In the same way how is below read?
const mainScreen_t * const lvl_1[LCD_MAIN_MENUS] = {&Screen1, &Screen2};
lvl_1 is a const pointer of type const mainScreen_t ?
In both variants, lvl_1[0] is a pointer to an entity of type "const mainScreen_t". In the second variant each lvl_1 array entry (word in memory containing the pointer) is also const.
With the first variant you could assign e.g. "lvl_1[0] = &Screen2", but with the second you couldn't. In neither case could you assign e.g. "lvl_1[0]->position = 3".
**Attention** This is a public forum