Nairou
Nairou

Reputation: 3755

GCC - Missing braces around initializer

There are lots of questions about this warning, but none of the ones I've tried seem to make the warning go away.

This is what I have:

typedef struct {
    union {
        float data[16];
        float col_row[4][4];
    };
} matrix44;

// ...

matrix44 result = {0};

I'm trying to initialize the struct to zero, but can't get it to not give an error about it. This is being compiled as C11.

I've also tried other variations, some ridiculous:

matrix44 result = {{0}};
matrix44 result = { {0}, {0} };
matrix44 result = { {0}, { {0}, {0} } };

But of course they all give the warning.

If I reduce the struct to just the single-dimensional data array, then I can initialize it with {{0}} without warning. But reducing it to the two-dimensional col_row array still gives the warning either way.

Is there a right way to do this that avoids the warning? Or is the warning incorrect in this case?

Upvotes: 7

Views: 11619

Answers (1)

ouah
ouah

Reputation: 145829

Use:

matrix44 result = {{{0}}};

to avoid warnings with gcc. The first pair of {} for the structure, the second pair for the union and the third pair for the array.

Upvotes: 16

Related Questions