如何在C中初始化结构
我对我如何在C中初始化结构进行了评论,说它不起作用,也不汇编。
这就是我在C中创建和初始化一个结构的方式。
struct {
int a;
int b;
char arr[3];
.
.
.
} data = {
.a = 1,
.b = 2
};
main(){
/* do stuff */
}
这是我如何初始化结构并运行和编译的方式。但是我收到评论说这将为C ++编译,但对于C.如果不是,为什么它没有错误地编译?
I got a comment on how I initialize my struct in C, saying that It does not work and it does not compile either.
This is how I create and initialize a structure in C.
struct {
int a;
int b;
char arr[3];
.
.
.
} data = {
.a = 1,
.b = 2
};
main(){
/* do stuff */
}
This how I initialize my struct and It works and compiles. Yet I got a comment saying this would compile for c++ but not for C. Can someone ensure me that this correct alternative? If not why is it compiling with no errors?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为数据成员
arr
的声明中有一个错字。而不是您必须编写,
您可能不会初始化带有空括号的数组。因此,例如,写作
实际上足以
暗中写入数组,将用零开始初始化。
I think there is a typo in the declaration of the data member
arr
. Instead ofyou have to write
You may not initialize an array with empty braces. So write for example
In fact it is enough to write
The array implicitly will be initialized with zeroes.