Skip to content
Tutorials Center
My learning

Topics

C Reference ยท Types and Literals

Flexible array members in C

Reference for C99 flexible array members: declaring a trailing array[], allocating with malloc and offsetof, copying rules, and the old [1] hack.

A flexible array member is an array with no size declared as the last member of a struct: int items[];. It lets a single allocation hold a header and a variable amount of data right behind it. This is the standard way to write a counted buffer or a string with a stored length in one block of memory.

The struct itself has a fixed size that does not include the flexible part. You decide how many elements you need when you allocate, so you cannot create such a struct on the stack with real elements; you allocate it with malloc.

C
struct list { size_t count; int items[]; };
struct list *l = malloc(sizeof *l + 5 * sizeof l->items[0]);

Try it

C A counted list in one allocation

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

C A string with its length attached

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Syntax

C
struct name {
    /* at least one other named member */
    type member;
    type array[];            /* must be last, incomplete array type */
};

size_t bytes = sizeof(struct name) + n * sizeof(type);
/* or, avoiding trailing padding waste: */
size_t bytes2 = offsetof(struct name, array) + n * sizeof(type);
Part Description
array[] Declares the flexible member. Only allowed as the last member and only in a struct with at least one other member.
sizeof(struct name) Size of the struct without the array (possibly including padding before it).
offsetof(struct name, array) Where the array starts; may be smaller than sizeof, so it can save a few bytes.

Errors / undefined behavior: accessing an element beyond what you allocated is undefined behavior. A struct with a flexible array member cannot be a member of another struct or an element of an array (GCC allows it as an extension in some cases). Assigning one such struct to another copies only the fixed part.

Usage notes

  • Allocate on the heap: malloc, calloc or realloc. Check for nullptr and for overflow in n * sizeof(type) (see ckd_mul).
  • Copy by hand. *a = *b copies only the fixed members. Use memcpy with the full size.
  • Do not put sizeof on the member. sizeof l->items is a constraint violation because the type is incomplete; keep the element count in another member.
  • Resizing: realloc the whole block, then update the count.
  • Pre-C99 hack: int items[1]; or items[0] worked in practice but is undefined behavior when indexed beyond the declared length. Modern compilers (with -fstrict-flex-arrays) treat them as real bounds.
  • Compare with a pointer member: int *items needs a second allocation and a second free, but allows separate resizing and copying by assignment.
  • C23 keeps the feature unchanged; the counted_by attribute is a compiler extension for bounds checking, not standard C.