问题
I am extremely new to C. Would appreciate if someone can help understand why code in lines 13,14 and 16 does not work, but lines 17-20 works.
With the first option (lines 13, 14 and 16) I get the error
error: initializer element is not constant
What does this mean? Also, does this mean one cannot use variables of certain type to generate new variables?
Thank you.
// Define structure for a good
5 struct good {
6 char goodname;
7 double p; //starting proportion
8 int theta; //average utility
9 int sigma; //variance of error
10 };
11
12 // The goods H and L
13 struct good H = {.goodname = 'H', .p = 0.5, .theta = 100, .sigma = 20};
14 struct good L = {.goodname = 'L', .p = 0.5, .theta = 75, .sigma = 20};
15
16 struct good goods[2] = {H, L}; // **Does not work**
// ** Works**
17 struct good goods[2] = {
18 {.goodname = 'H', .p = 0.5, .theta = 100, .sigma = 20},
19 {.goodname = 'L', .p = 0.5, .theta = 75, .sigma = 20}
20 };
回答1:
H and L are storage locations that contain data. The statement:
struct good goods[2] = {H, L}; // **Does not work**
implies that goods should point to H and L, or contain the same values as H and L.
Either, copy the data from H and L to goods[0] and [1] or modify goods to be an array of pointers, as:
struct good *goods[2];
goods[0] = &H;
goods[1] = &L;
回答2:
The reason line 16 does not work is the same why this much simpler example would not work:
const int a = 5;
int b = a; // Does not work: "initializer element is not constant"
What this means is that you need to use only compile-time constant expressions in the initializers. C does not consider variables compile-time constants, even the const ones (C++, on the other hand, considers const variables compile-time constants).
回答3:
It means in C you can only use constant expressions to initialize an array or a structure with static storage duration.
In your example all objects have static storage duration but:
struct good H = {.goodname = 'H', .p = 0.5, .theta = 100, .sigma = 20};
the initializers above are literals and literals are constant expressions in C, so it is OK.
struct good goods[2] = {H, L};
but here H and L objects are not constant expressions so the compiler gives you an error.
C has a rather strict definition of what a constant expression is and the value of an object is not considered a constant expression (even if the object is const qualified or initialized with a literal).
回答4:
The value of a variable, even if the variable is already initialized globally1 (and even if it is const-qualified), is not itself a constant-expression.
A pointer to a static-duration variable2 is an "extended constant expression" and can be used to initialize pointer variables, so you could do:
struct good *goods[2] = {&H, &L};
for instance, if that suits the problem.
1Yes, I know "initialized globally" is not really well-defined. :-) I mean, if it's a static-duration variable that has been initialized, and is visible at the point of the new initializer.
2I.e., any "global" (file scope) variable, or a static variable inside a block.
回答5:
If the object is static "goods[0]/goods[1]", you can only initialize with constants. So, the reason you did not get an error initializing "good H".
If you define your variable as non-static, (for e..g, in your main() or in any of your function()) you will not get this error, as the object will be treated as "auto".
来源:https://stackoverflow.com/questions/18495108/initializing-an-array-of-structs-why-does-using-struct-variables-not-work