There are a few problems with
struct S {
int num;
int data[num];
};
which make it work not the way you want.
-, num, , num, struct; num (.. num , struct) 1.
, struct union 2. :
struct S {
int num;
int data[];
};
, ; struct S,
struct S foo;
. :
struct S *foo = malloc( sizeof *foo + N * sizeof *foo->arr );
. : struct S , . 3
, -
struct S {
size_t num;
int *data;
};
data struct:
struct S foo;
foo.num = some_value();
foo.data = malloc( sizeof *foo.data * foo.num );
struct S , , :
struct S blah[10];
struct T {
struct S s;
...
};
1. C - ( ), struct/union/enum ( struct, union enum)), struct union ( . ->) . num . ->, .
2. 6.7.2.1/9: " , ".
2. 6.2.7.1/3: (,
,
), ,
; ( , , ,
, , )
.