Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

-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?

like image 486
Lundin Avatar asked Nov 02 '22 03:11

Lundin


1 Answers

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}
};
like image 52
Henrik Avatar answered Nov 15 '22 06:11

Henrik