djadmin
djadmin

Reputation: 1760

Zero-Length Arrays

What is the advantage of using zero-length arrays in C?

Eg:

struct email {
    time_t send_date;
    int flags;
    int length;
    char body[];
}list[0];

Upvotes: 2

Views: 238

Answers (2)

ouah
ouah

Reputation: 145899

An array of size 0 is not valid in C.

char bla[0];  // invalid C code

From the Standard:

(C99, 6.7.5.2p1) "If the expression is a constant expression, it shall have a value greater than zero."

So list declaration is not valid in your program.

An array with an incomplete type as the last member of a structure is a flexible array member.

struct email {
    time_t send_date;
    int flags;
    int length;
    char body[];
};

Here body is a flexible array member.

Upvotes: 4

Related Questions