...
Code Block | ||
---|---|---|
| ||
struct flexArrayStruct {
int num;
int data[1];
};
/* ... */
/* Space is allocated for the struct */
struct flexArrayStruct *structP = (struct flexArrayStruct *)malloc(sizeof(struct flexArrayStruct) + sizeof(int) * (ARRAY_SIZE - 1));
if (!structP) {
/* handle malloc failure */
}
structP->num = SOME_NUMBER;
/* Access data[] as if it had been allocated as data[ARRAY_SIZE] */
for (i = 0; i < ARRAY_SIZE; i++) {
structP->data[i] = i;
}
|
...
Code Block | ||
---|---|---|
| ||
struct flexArrayStruct{ int num; int data[]; }; /* ... */ /* Space is allocated for the struct */ struct flexArrayStruct *structP = (struct flexArrayStruct *)malloc(sizeof(struct flexArrayStruct) + sizeof(int) * ARRAY_SIZE); if (!structP) { /* handle malloc failure */ } structP->num = SOME_NUMBER; /* Access data[] as if it had been allocated as data[ARRAY_SIZE] */ for (i = 0; i < ARRAY_SIZE; i++) { structP->data[i] = i; } |
...