Flexible array members are a special type of array where the last element of a structure with more than one named member has an incomplete array type; that is, the size of the array is not specified explicitly within the structure. If a dynamically sized structure is needed, flexible array members should be usedA variety of different syntaxes have been used for declaring flexible array members. For C99-compliant implementations, use the syntax guaranteed valid by C99.
Non-Compliant Code Example
...
However, some restrictions do apply:
- The incomplete array type must be the last element within the structure.
- You cannot have an array of structure if the structure contains flexible array members.
- Structures that contain a flexible array member cannot be used as a member in the middle of another structure.
- You cannot apply the
sizeof
operator to a flexible array.
Risk Assessment
Although the non-compliant approach results in undefined behavior, it does work under most architectures.
...