Array of size 0 at the end of struct [duplicate]

Currently, there exists a standard feature, as mentioned in C11, chapter ยง6.7.2.1, called flexible array member.

Quoting the standard,

As a special case, the last element of a structure with more than one named member may
have an incomplete array type; this is called a flexible array member. In most situations,
the flexible array member is ignored. In particular, the size of the structure is as if the
flexible array member were omitted except that it may have more trailing padding than
the omission would imply. […]

The syntax should be

struct s { int n; double d[]; };

where the last element is incomplete type, (no array dimensions, not even 0).

So, your code should better look like

struct array{
    size_t size;
    int data[ ];

to be standard-conforming.

Now, coming to your example, of a 0-sized array, this was a legacy way (“struct hack”) of achieving the same. Before C99, GCC supported this as an extension to emulate flexible array member functionality.

Leave a Comment