I just came across some code which declares a struct within a C++ class as follows:
struct T
{
int data;
T* next;
} array[0];
What does this kind of declaration do? What effect does putting “array[0]” at the end of the struct defintion have?
Ahh, it’s almost a “flexible array member”.
It was formalized in C99, but it’s an old C trick that creates a dynamic array. Allocate more memory for the object and you can store more elements in the array.
I think the use of a specific
0there is a compromise, as actual[]flexible arrays were not in C89 or C++. The more canonical legacy use of this pattern is...[1].