我对 C 非常陌生。如果有人能帮助理解为什么第 13,14 和 16 行中的代码不起作用,但第 17-20 行起作用,我将不胜感激。
使用第一个选项(第 13、14 和 16 行)我得到错误
error: initializer element is not constant
这是什么意思?另外,这是否意味着不能使用某种类型的变量来生成新变量?
谢谢你。
// 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 };