The following code wont compile:
const int a = 0;
struct Test
{
int b;
};
static const struct Test test =
{
a
};
Its a cut down example of what I am really trying to do, but what am I doing wrong?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
In C89/90 version of C language all aggregate initializers must consist of constants only. In C language terminology a constant of
inttype is a literal value, like10,20u,0x1etc. Enum members are also constants. Variables ofconst inttype are not constants in C. You can’t use aconst intvariable in aggregate initializer. (For this reason, in C language, when you need to declare a named constant you should use either#defineorenum, but notconstqualifier.)In C99 this requirement for aggregate initializers was relaxed. It is now OK to use non-constants in aggregate initializers of local objects. However, for static objects (as in your example) the requirement still holds. So, even in C99 you’l’ have to either use
or use a named enum constant as suggested in @R..’s answer.