Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

error: expression must have a constant value

I need some help to find how to resolve this error.

typedef struct {
    const char *iName;
    const char *iComment;
} T_Entry;

const T_Entry Menu_PowerSupply = { "PWRS", "Power supply"};

static const T_Entry G_Commands[] = {
    { "MEM", "Memory"},
    {Menu_PowerSupply.iName,Menu_PowerSupply.iComment},
    { "SYS", "System"}
};

I got the error : expression must have a constant value How can I solve this ?

For me at link time is known and at a fixed address with a fixed value : Am I wrong


My purpose is to put the following code into a library

const T_Entry Menu_PowerSupply = { "PWRS", "Power supply"};

The following not work either

static const T_Entry G_Commands[] = {
    { "MEM", "Memory"},
    Menu_PowerSupply,
    { "SYS", "System"}
};

If someone could help me to understand this non const values ...

like image 544
user3274807 Avatar asked Mar 19 '23 15:03

user3274807


1 Answers

The error is because the initializer for global variables must be constant expression, but even though Menu_PowerSupply is defined as const, it's not a constant expression.

This is similar to:

const int n = 42;
int arr[n]; 

doesn't compile in C89 because n is not a constant expression. (it does compile in C99 only because C99 supports VLA)

like image 197
Yu Hao Avatar answered Mar 28 '23 03:03

Yu Hao