预计……当我有一个时,在声明列表的末尾?

发布于 2025-01-15 21:01:22 字数 363 浏览 3 评论 0原文

所以我声明了一个如下所示的结构,

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;

so i've declared a struct which is shown below

struct location {
    char occupier;
    int points;
    int x;
    int y;
    int current_x = PLAYER_STARTING_COL;
    int current_y = PLAYER_STARTING_ROW;
    
};

I have made a struct variable called starting however, when I compile it I get an error saying it expects a semicolon at the end of my declaration list? Is there any way I can fix this? I'm a beginner to C and just need a bit of help

struct location starting;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冰葑 2025-01-22 21:01:22

与 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_COLPLAYER_STARTING_ROW 必须是常量表达式。

In C opposite to C++ you may not initialize data members of a structure in its definition.

So you have to write

struct location {
    char occupier;
    int points;
    int x;
    int y;
    int current_x;
    int current_y;
    
};

and to initialize data members when an object of the structure type is defined as for example

struct location starting = 
{ 
    .current_x = PLAYER_STARTING_COL, .current_y = PLAYER_STARTING_ROW 
};

If the above declaration is a file scope declaration then PLAYER_STARTING_COL and PLAYER_STARTING_ROW must be constant expressions.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文