Let's assume I have a structure defined as:
typedef struct _TStruct {
uint Values[3];
} TStruct;
Then I define an array of structures:
TStruct Data[3];
How do I correctly initialize the arrays in this array of structures?
Let's assume I have a structure defined as:
typedef struct _TStruct {
uint Values[3];
} TStruct;
Then I define an array of structures:
TStruct Data[3];
How do I correctly initialize the arrays in this array of structures?
To correctly initialize an array in an array of structures you need to do the following:
typedef struct _TStruct {
uint Values[3];
} TStruct;
TStruct Data[3] = {
{{ 0x86, 0x55, 0x79 }}, {{ 0xaa, 0xbb, 0xcc }}, {{ 0x76, 0x23, 0x24 }}
};
Pay attention to the double braces around every group of values. The additional pair of braces is essential to avoid getting a following gcc error (only when -Wall
flag is present, precisely it's "detected" by gcc
-Wmissing-braces
flag):
warning: missing braces around initializer
Note:
Usage of double braces {{ }}
does not change the layout of data in memory
This warning does not appear on MS Visual Studio C++ compiler
See also: