0

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?

Peter Cerba
  • 806
  • 4
  • 14
  • 26
  • The cited dup and [GCC 53119 bug](https://gcc.gnu.org/bugzilla/show_bug.cgi?id=53119) seems to surface when using `{0}` with missing braces. This question does not appear to be initializing that way. – jww Feb 19 '17 at 21:13

1 Answers1

2

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:

  1. Usage of double braces {{ }} does not change the layout of data in memory

  2. This warning does not appear on MS Visual Studio C++ compiler

See also:

How to repair warning: missing braces around initializer?

GCC Bug 53319

Community
  • 1
  • 1
Peter Cerba
  • 806
  • 4
  • 14
  • 26