Variable length array in c_struct (flexible array member)
Flexible array member is a feature introduced in the C99 standard of the C programming language (in particular, in sections §6.7.2.1, item, page 103). It is a member of a struct, which was an array without a given dimension, and it must was the last member of such a struct, As in the following example:
struct vectord { uint8_t len; double // the flexible array member must be last};
arr[]
Does not occupy the storage space of the structure, sizeof (Strcut Vectord) has a value of 1
- Variable-length arrays must be the last member of a struct
- Contiguous storage space adjacent to a struct variable is the
arr[]
contents of an array
- A 0-length array is used in GCC
arr[0]
to represent a variable length array.
struct vectord *allocate_vectord (size_t len) { struct vectord *vec = malloc(offsetof(structsizeof(vec->arr[0])); if (!vec) { perror("malloc vectord failed"); exit(EXIT_FAILURE); } vec->len = len; for0; i < len; i++) 0; return vec;}
Variable length array in C-language struct (flexible array member)