Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GCC - Missing braces around initializer

Tags:

c

gcc

c11

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?

like image 215
Nairou Avatar asked Jan 25 '15 19:01

Nairou


1 Answers

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.

like image 132
ouah Avatar answered Nov 13 '22 06:11

ouah