示例代码:
#include <stdio.h>
typedef struct
{
unsigned int a,
unsigned int b,
unsigned int c
} user_struct;
int main()
{
user_struct arr[5] = {0}; // gives warning on compilation
return 0;
}
上面的代码在gcc5.4中给出了警告 下面是警告。
警告:初始化器周围缺少大括号
我的理解是,如果我想将任何对象初始化为 0,我可以等同于 {0}。
如何将结构数组初始化为 0 而不会出现编译器警告?谢谢。
最佳答案
代码的唯一问题是每个结构元素后面需要分号 ;
而不是 ,
。
typedef struct
{
unsigned int a;
unsigned int b;
unsigned int c;
} user_struct;
你的初始化没问题:
user_struct arr[5] = {0}; // this should memset all array elements (here struct) to zero
警告(如果有)只是您已初始化但从未在代码中使用过 arr
。
关于c - C 中结构体数组初始化为 0,警告 : missing braces around initializer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41051053/