我正在使用GCC 4.6.2(Mingw)并使用-Wextra
进行编译。每当我使用指定的初始化程序时,我都会收到奇怪的警告。对于以下代码
typedef struct
{
int x;
int y;
} struct1;
typedef struct
{
int x;
int y;
} struct2;
typedef struct
{
struct1 s1;
struct2 s2[4];
} bug_struct;
bug_struct bug_struct1 =
{
.s1.x = 1,
.s1.y = 2,
.s2[0].x = 1,
.s2[0].y = 2,
.s2[1].x = 1,
.s2[1].y = 2,
.s2[2].x = 1,
.s2[2].y = 2,
.s2[3].x = 1,
.s2[3].y = 2,
};
我收到警告
bug.c:24:3: warning: missing initializer [-Wmissing-field-initializers]
bug.c:24:3: warning: (near initialization for 'bug_struct1.s1.y') [-Wmissing-field-initializers]
究竟缺少什么?我已经初始化了每个成员。这个警告是否过于生硬,无法使用指定的初始化程序,我做错了什么,还是编译错误?
答案 0 :(得分:2)
正如你所说,警告似乎“太生硬了”。
这种访问模式,初始化每个成员结构作为一个整体,满足编译器:
bug_struct bug_struct1 =
{
.s1 = {.x = 1, .y = 2},
.s2[0] = {.x = 1, .y = 2},
.s2[1] = {.x = 1, .y = 2},
.s2[2] = {.x = 1, .y = 2},
.s2[3] = {.x = 1, .y = 2}
};