所以我声明了一个结构,它显示在下面
struct location {
char occupier;
int points;
int x;
int y;
int current_x = PLAYER_STARTING_COL;
int current_y = PLAYER_STARTING_ROW;
};
我已经创建了一个名为starting的结构变量,但是当我编译它时,我收到一个错误,说它需要在声明列表的末尾使用分号?有什么办法我能解决这个问题吗?我是C的初学者,只需要一些的帮助
struct location starting;
在与C++相反的C中,您可能无法在其定义中初始化结构的数据成员。
所以你必须写
struct location {
char occupier;
int points;
int x;
int y;
int current_x;
int current_y;
};
并且当结构类型的对象被定义为例如时初始化数据成员
struct location starting =
{
.current_x = PLAYER_STARTING_COL, .current_y = PLAYER_STARTING_ROW
};
如果上面的声明是文件作用域声明,那么PLAYER_STARTING_COL
和PLAYER_STARTING_ROW
必须是常量表达式。