Jes*_*hen 2 c struct c99 hardcoded
我在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},
}
};
Run Code Online (Sandbox Code Playgroud)
它给出了错误:
error: expected ‘:’, ‘,’, ‘;’, ‘}’ or ‘__attribute__’ before ‘=’ token
Run Code Online (Sandbox Code Playgroud)
我希望能够访问数据,例如在结构中具有结构:
table[row].pos[column].piece
table[row].pos[column].alive
Run Code Online (Sandbox Code Playgroud)
我尝试了几种组合,似乎没有一种组合适用于这种情况.在此之前我已经完成了之前的struct硬编码初始化,但这次不是结构中的结构.
有什么建议?
尝试将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}}}
};
Run Code Online (Sandbox Code Playgroud)