Is empty array in the end of the structure a C standard? -
i have noticed empty array in end of structure used in open source projects:
typedef struct { ...... void *arr[]; } a;
i want know c standard? or ok gcc compiler?
as of c99, c standard. pre-c99 compilers may not support it. old approach declare 1-element array, , adjust allocation size that.
new way:
typedef struct { ...... void *arr[]; } a; int slots = 3; a* mya = malloc(sizeof(a) + slots*sizeof(void*)); mya->arr[2] = foo;
old way:
typedef struct { ...... void *arr[1]; } a; int slots = 3; a* mya = malloc(sizeof(a) + (slots-1)*sizeof(void*)); mya->arr[2] = foo;
Comments
Post a Comment