-Wmissing-field-initializer when using designated initializers

别来无恙 提交于 2019-12-18 08:47:43

问题


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

I get warnings

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?


回答1:


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


来源:https://stackoverflow.com/questions/22194935/wmissing-field-initializer-when-using-designated-initializers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!