-Name field when using the assigned initializers

I am using GCC 4.6.2 (Mingw) and compiling with -Wextra. I get strange warnings when 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 initialized each member. Is this warning too dumb to work with designated initializers, am I doing something wrong or is this a compiler error?

+4
source share
1 answer

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

This access pattern, initializing each member structure 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}
};
+2

Source: https://habr.com/ru/post/1530220/


All Articles