gpt4 book ai didi

c - -Wmissing-field-initializer 使用指定的初始值设定项时

转载 作者:太空狗 更新时间:2023-10-29 15:38:35 26 4
gpt4 key购买 nike

我正在使用 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]

那么到底缺少了什么?我已经初始化了每个成员。这个警告是否过于生硬而无法使用指定的初始化程序,我做错了什么,还是编译器错误?

最佳答案

看来,正如您所说,警告“太生硬了”。

这种访问模式,将每个成员结构初始化为一个整体,满足编译器:

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}
};

关于c - -Wmissing-field-initializer 使用指定的初始值设定项时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22194935/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com