I need to return a structure with a flexible array member from a C function but can\'t figure out why it doesn\'t compile. I know that returning arrays can be achieved by en
Structs with flexible array members like yours (where the size of data
is not given in the struct definition) are to be allocated like that:
struct data_array *a; // pointer!!
a = malloc( sizeof(struct data_array) + 1000 * sizeof(double) );
Where sizeof(struct data_array)
is the constant size of your struct excluding data
and 1000
is the desired array size.
Of course changing data
to be a pointer and allocating it seperately will also work, but that is something slightly different.