I found a GNU C documentation on flexible arrays, and they say that you can initialize them like that:
struct foo { int x; int y[]; };
struct bar { struct foo z; };
struct foo a = { 1, { 2, 3, 4 } }; // Valid.
struct bar b = { { 1, { 2, 3, 4 } } }; // Invalid.
struct bar c = { { 1, { } } }; // Valid.
struct foo d[1] = { { 1, { 2, 3, 4 } } }; // Invalid
But when I try to do the first valid case (with struct a
), gcc throws error: Initialization of flexible array member is not allowed
. So is this deprecated or is there another way to do it without malloc
?