Why do I need more curly braces when initializing this structure?

First I tried to initialize the structure as follows:

struct { char age[2]; // Hold two 1-Byte ages } studage[] = { {23, 56}, {44, 26} }; 

But this gives me a compiler warning about missing curly braces, so I used more brackets, as suggested by the compiler, and in the end it turned out:

 struct { char age[2]; // Hold two 1-Byte ages } studage[] = { {{23, 56}}, {{44, 26}} }; 

There are no warnings. Why do I need extra curly braces?

+5
source share
1 answer

You have an array of structures, the structure has one element, which is an array.

 struct { char age[2]; // Hold two 1-Byte ages } studage[] = { ^ This is for the studage array { { 23, 56}}, ^ ^ | this is for the age array this is for the anonymous struct {{44, 26}} }; 

It might be easier for you to see if your structure has another member:

 struct { int id; char age[2]; } studage[] = { {1, {23, 56}}, ^ ^ ^ id | | age[0] | age[1] }; 
+10
source

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


All Articles