[英]C struct within a struct hardcoded initialization
What I am doing wrong here for C99: 我在C99做错了什么:
struct chess {
struct coordinate {
char piece;
int alive;
} pos[3];
}table[3] =
{
{
{'Q', (int)1},{'Q', (int)1},{'Q', (int)1},
{'B', (int)1},{'B', (int)1},{'B', (int)1},
{'K', (int)1},{'K', (int)1},{'K', (int)1},
}
};
It gives the error: 它给出了错误:
error: expected ‘:’, ‘,’, ‘;’, ‘}’ or ‘__attribute__’ before ‘=’ token
I wish to be able to access the data like having a struct within a struct that: 我希望能够访问数据,例如在结构中具有结构:
table[row].pos[column].piece
table[row].pos[column].alive
I tried several combinations , and none seems to work for this case. 我尝试了几种组合 ,似乎没有一种组合适用于这种情况。 I have done previous struct hard coded initialization before that works, but not a struct within a struct as this time.
在此之前我已经完成了之前的struct硬编码初始化,但这次不是结构中的结构。
Any suggestions? 有什么建议么?
Try to enclose char literals in single quotes as stated above and add extra braces to make inner arrays to be initializer lists. 尝试将char文字括在单引号中,如上所述,并添加额外的大括号,使内部数组成为初始化列表。
struct chess
{
struct coordinate
{
char piece;
int alive;
} pos[3];
}
table[3] =
{ // table of 3 struct chess instances...
{ // ... start of a struct chess with a single member of coordinate[3]...
{ // ... this is where the coordinate[3] array starts...
// ... and those are the individual elements of the coordinate[3] array
{'Q', 1}, {'Q', 1}, {'Q', 1}
}
},
{{{'B', 1}, {'B', 1}, {'B', 1}}},
{{{'K', 1}, {'K', 1}, {'K', 1}}}
};
struct chess {
struct coordinate {
char piece;
int alive;
} pos[3];
} table[3] =
{
{
.pos = {{ .piece = 'Q', .alive = 1 },
{ .piece = 'Q', .alive = 1 },
{ .piece = 'Q', .alive = 1 }
}
},
{
.pos = {{ .piece = 'B', .alive = 1 },
{ .piece = 'B', .alive = 1 },
{ .piece = 'B', .alive = 1 }
}
},
{
.pos = {{ .piece = 'K', .alive = 1 },
{ .piece = 'K', .alive = 1 },
{ .piece = 'K', .alive = 1 }
}
}
};
It seems to work. 它似乎工作。 Just be careful about the placement of your braces, and PLEASE try to understand what you are typing.
请小心放置大括号,并尝试了解您输入的内容。 This is how to read the answer :
这是如何阅读答案:
Advices : 建议:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.