使用指定的初始化时-Wmissing场初始值设定 [英] -Wmissing-field-initializer when using designated initializers

查看:1486
本文介绍了使用指定的初始化时-Wmissing场初始值设定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用GCC 4.6.2(称为Mingw)和 -Wextra 编译。我得到每当我使用指定初始化奇怪的警告。对于以下code

I'm using GCC 4.6.2 (Mingw) and compiling with -Wextra. I'm getting strange warnings whenever I use designated initializers. For the following code

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]

那么到底缺什么?我已经初始化的每一个成员。这是警告只是太钝指定的初始化工作,我做错了什么,或者是一个编译器错误吗?

So what exactly is missing? I've initialized every member. Is this warning merely too blunt to work with designated initializers, am I doing something wrong, or is it a compiler bug?

推荐答案

看来,警告,就像你说的,太钝。

It seems that the warning is, as you say, "too blunt".

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

This pattern of access, initializing each member struct as a whole, satisfies the compiler:

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

这篇关于使用指定的初始化时-Wmissing场初始值设定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆