...
Code Block | ||
---|---|---|
| ||
struct flexArrayStruct{ int num; char my_char; int data[]; }; ... /* Space is allocated for the struct */ struct flexArrayStruct *structP = malloc(sizeof(struct flexArrayStruct) + sizeof(int) * ARRAY_SIZE); if (!structP) { /* handle malloc failure */ } /* Now, it is as if the struct were defined struct {int num; char my_char; int data[ARRAY_SIZE];} *structP; and we can access the elements as if they were so. */ structP->num = SOME_NUMBER; structP->my_char = SOME_CHAR; /* Access data[] as if array had been allocated as data[ARRAY_SIZE] */ for (i = 0; i < ARRAY_SIZE; i++) { structP->data[i] = i; } |
...