Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Warning when trying to initialize a 2D struct array with two initializer lists

I am trying to initialize a 2D array of user defined type to zero by using following line,

qmf_t X_hybrid_left[32][32] = {{0}};

Where qmf_t is a user defined type. Here I get the compiler warning,

warning: missing braces around initializer [-Wmissing-braces]"

But if I use, qmf_t X_hybrid_left[32][32] = {{{0}}};, i.e. 3 braces each side, warning disappears.

Is it correct to use three braces on each side? What does it mean?

like image 520
user2389323 Avatar asked Jun 10 '15 07:06

user2389323


1 Answers

qmf_t X_hybrid_left[32][32] = {  /* Row initializers next */
                                { /* Col initializers next */
                                  { /* Struct initializers next */
                                     0
                                  }
                                }
                              };
qmf_t a = {0};
qmf_t b[5] = {{0}};
qmf_t c[10][5] = {{{0}}};

From C11 specs, 6.7.9 Initialization grammar

initializer:
assignment-expression
{ initializer-list }
{ initializer-list , }

Although in your particular case (zeroing all objects of 2 arrays), qmf_t X_hybrid_left[32][32] = {0}; will work same as qmf_t X_hybrid_left[32][32] = {{{0}}}; but compiler may warn you.

But if you want any non-zero initialization, you need to use multiple braces.

From the same section:

[16] Otherwise, the initializer for an object that has aggregate or union type shall be a brace enclosed list of initializers for the elements or named members.

[20] If the aggregate or union contains elements or members that are aggregates or unions, these rules apply recursively to the subaggregates or contained unions. If the initializer of a subaggregate or contained union begins with a left brace, the initializers enclosed by that brace and its matching right brace initialize the elements or members of the subaggregate or the contained union. Otherwise, only enough initializers from the list are taken to account for the elements or members of the subaggregate or the first member of the contained union; any remaining initializers are left to initialize the next element or member of the aggregate of which the current subaggregate or contained union is a part.

like image 106
Mohit Jain Avatar answered Nov 09 '22 22:11

Mohit Jain