Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
751 views
in Technique[技术] by (71.8m points)

c - -Wmissing-field-initializer when using designated initializers

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...