我有一个结构包含另一个结构的数组,它看起来像这样:
typedef struct bla Bla; typedef struct point Point; struct point { int x, y; }; struct bla { int another_var; Point *foo; };
现在我想在全局范围内初始化它们。它们旨在作为模块的描述。我试着用c99复合文本来实现这一点,但编译器(gcc)不喜欢它:
Bla test = { 0, (Point[]) {(Point){1, 2}, (Point){3, 4}} };
我得到以下错误:
error: initializer element is not constant error: (near initialization for 'test')
每个元素不需要复合文字,只需创建一个复合文字数组:
Bla test = { 0, (Point[]) {{1, 2}, {3, 4}} };
确保编译时使用 -std=c99
-std=c99