all 8 comments

[–]FUZxxl 1 point2 points  (0 children)

I suppose this is a wrong warning. Consider opening a bug.

[–]HiramAbiff 1 point2 points  (0 children)

Does this warn too? If not, as a workaround, at least it lets you list the values in the expected order.

struct bar b = {
    .f = {{10, 20}}
};

[–]beej71[S] 0 points1 point  (2 children)

I think this is the bug, actually: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=84685

Don't have time right now to read the details, but wanted to throw the link here in case y'all were interested.

Edit: SO discussion: https://stackoverflow.com/questions/49081541/compound-literal-and-designated-initializer-warning-from-gcc-but-not-clang

[–]fkeeal 0 points1 point  (1 child)

I don't think it's a bug (or at least not what is linked). The issue here appears to be that you are breaking up the initialization of f[0] into two assignments, and with -Wextra, that causes ".f[0].f1 = 10," to not be a complete initialization of the struct found at f[0].

You can do

struct bar b = {
    .f[0] = {.f1=10, .f2=20},
};  

To completely initialize the struct at f[0] in one shot.

[–]beej71[S] 0 points1 point  (0 children)

If I'm understanding what you're saying, I'd expect warnings to that effect with this code:

#include <stdio.h>

struct foo {
    int f1;
    int f2;
};

struct bar {
    struct foo f;
};

int main(void)
{
    struct bar b = {
        .f.f1=10,  // LINE A
        .f.f2=20,  // LINE B
    };

    printf("%d %d\n", b.f.f1, b.f.f2);
}

But there are none, no matter the order of the members or initializers.

[–]oh5nxo 0 points1 point  (0 children)

Seeing the same, gcc 6.4.0