Initialization of nested structures / arrays

I have a structure that contains arrays of another structure, it looks something like this:


typedef struct bla Bla;
typedef struct point Point;

struct point
{
    int x, y;
};

struct bla
{
    int another_var;
    Point *foo;
};

Now I want to initialize them in a global area. They are intended to describe the module. I tried to do this with c99 complex literals, but the compiler (gcc) didn't like this:


Bla test =
{
    0, (Point[]) {(Point){1, 2}, (Point){3, 4}}
};

I get the following errors:

error: initializer element is not constant
error: (near initialization for 'test')

Since I don’t need to change it, I can put as much β€œconst” in it as necessary. Is there any way to compile it?

+5
source share
1 answer

You do not need a composite literal for each element, just create a single array of literals:

Bla test =
{
    0, (Point[]) {{1, 2}, {3, 4}}
};

, -std=c99.

+5

All Articles