嵌套结构/数组初始化

qui*_*ars 5 c compound-literals

我有一个包含另一个结构的数组的结构,它看起来像这样:


typedef struct bla Bla;
typedef struct point Point;

struct point
{
    int x, y;
};

struct bla
{
    int another_var;
    Point *foo;
};
Run Code Online (Sandbox Code Playgroud)

我现在想在全球范围内初始化它们.它们旨在作为模块的描述.我尝试用c99复合文字做到这一点,但编译器(gcc)不喜欢它:


Bla test =
{
    0, (Point[]) {(Point){1, 2}, (Point){3, 4}}
};
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

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

由于我不需要修改它,我可以根据需要在其中加入尽可能多的"const".有没有办法编译它?

Rob*_*ble 5

您不需要每个元素的复合文字,只需创建一个复合文字数组:

Bla test =
{
    0, (Point[]) {{1, 2}, {3, 4}}
};
Run Code Online (Sandbox Code Playgroud)

确保你编译-std=c99.