4
#include <stdio.h>

typedef struct
{
    int as;
    int bs;
    int cs;
}asd_t;

typedef struct
{
    asd_t asd[10];
}asd_field_t;    

typedef struct
{
    int a;
    int b;
    asd_field_t  asd_field[10];
}abc_t;    

int main()
{
    abc_t abc ={0,1,{0}};
    return 0;
}

在上面的代码中,我试图初始化结构abc_t。将上面的代码编译为:

gcc -Wall sample.c

给我:

sample.c: In function 'main':
sample.c:26: warning: missing braces around initializer
sample.c:26: warning: (near initialization for 'abc.asd_field[0].asd')

如何避免此警告?

4

4 回答 4

5

结构 abc_t 在 asd_field_t 类型内有另一个结构,您可以使用 {0} 将其初始化为 0。您从 GCC 收到的警告是因为您将该结构 (asd_field) 的所有成员归零,而不是一一填充它们。有一种说法认为 GCC 的这种行为是不正确的,因为标准认为使用 {0} 将整个结构归零是完全有效的。你可以在这里阅读 GCC 的错误报告

您还可以通过传递选项 -Wno-missing-braces 来禁用烦人的警告,以便获得所有其他墙壁警告,即: gcc -Wall -Wno-missing-braces test.c -o test

于 2013-03-26T09:21:30.400 回答
2

尝试这个:

abc_t abc ={0,1,{{{{0,0,0}}}}};
于 2013-03-26T09:36:40.250 回答
0

好吧,您必须在 struct 中嵌套数组abc_t,因此,您必须执行以下操作:

abc_t abc = {0,1,{{0,0,0}}};
于 2013-03-26T10:20:35.290 回答
-1

abc_t abc ={0,1,{ {{{0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0} , {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}}},{{{0,0, 0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0} , {0,0,0}, {0,0,0}, {0,0,0}}},{{{0,0,0}, {0,0,0}, {0,0, 0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0} , {0,0,0}}},{{{0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0, 0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0}}},{{{0, 0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0, 0}, {0,0,0}, {0,0,0}, {0,0,0}}},{{{0,0,0}, {0,0,0}, {0, 0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0, 0}, {0,0,0}}},{{{0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0, 0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}}},{{{ 0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0, 0,0}, {0,0,0}, {0,0,0}, {0,0,0}}},{{{0,0,0}, {0,0,0}, { 0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0, 0,0}, {0,0,0}}},{{{0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0, 0},{0,0,0},{0,0,0},{0,0,0}}} }};

于 2013-03-26T11:11:23.287 回答