Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
bgColor#FFcccc
struct flexArrayStruct {
     int num;
     char my_char;
     int data[1];
};

...
/* Space is allocated for the struct */
struct flexArrayStruct *structP = malloc(sizeof(struct flexArrayStruct) + sizeof(int) * (ARRAY_SIZE - 1));
if (!structP) {
     /* handle malloc failure */
}
structP->num = SOME_NUMBER;
structP->my_char = SOME_CHAR;

/* 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
bgColor#ccccff
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 */
}

structP->num = SOME_NUMBER;
structP->my_char = SOME_CHAR;

/* Access data[] as if it had been allocated as data[ARRAY_SIZE] */
for (i = 0; i < ARRAY_SIZE; i++) {
  structP->data[i] = i;
}

...