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 };