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?

1
  • The cited dup and GCC 53119 bug seems to surface when using {0} with missing braces. This question does not appear to be initializing that way. Commented Feb 19, 2017 at 21:13

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

3 Comments

Your Q & A is a duplicate of the linked Q & A you provided. Why posting that then?
@Jean-FrançoisFabre There was no question/answer on SO showing how to initialize array in an array of structs
Note that omitting braces is also correct according to the C Standard. The extra bracing is only "required" to work around a compiler defect.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.