Next: Flexible Array Fields, Previous: const Fields, Up: Structures [Contents][Index]
GNU C allows zero-length arrays. They are useful as the last element
of a structure that is really a header for a variable-length object.
Here’s an example, where we construct a variable-size structure
to hold a line which is this_length
characters long:
struct line { int length; char contents[0]; }; struct line *thisline = ((struct line *) malloc (sizeof (struct line) + this_length)); thisline->length = this_length;
In ISO C90, we would have to give contents
a length of 1, which
means either wasting space or complicating the argument to malloc
.