So I have these 2 structures:
typedef struct item {
const char *label;
int value;
} Item;
typedef struct item_coll {
size_t length;
Item items[];
} ItemColl;
And I want to do this:
int main() {
Item a = {"A", 10};
Item b = {"B", 20};
Item c = {"C", 30};
Item items[] = {a, b, c};
size_t length = sizeof(items)/sizeof(items[0]);
ItemColl *column = malloc (sizeof(column) + length * sizeof(Item));
column -> length = length;
column -> items = items;
printf("%ld\n", column -> length);
return 0;
}
But I'm getting the error "Invalid use of flexible array member" here:
column -> items = items;
As far as I know, I'm allocating the needed space, which is why I don't understand what the problem is.
I've seen 2 more posts with this title but none of them solves my problem, as I've tried the answers to those questions.
=- flexible array member or otherwisesizeof(*collumn)? You are getting the size of a pointer.memcpyorfor (int idx = 0; idx < length; ++idx) collumn->items[idx] = items[idx];instead ofcollumn->items = items;sizeof(collumn)is just the size of a pointer, you need to usesizeof *collumninstead.