Versions Compared

Key

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

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:

  1. The incomplete array type must be the last element within the structure.
  2. You cannot have an array of structure if the structure contains flexible array members.
  3. Structures that contain a flexible array member cannot be used as a member in the middle of another structure.
  4. 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.

...