This array:
struct hello { int number; int data[]; };
it makes sense only as the last element of the structure and only when distributing the structure from the heap and, as a rule, so that the header fields contain the actual size of the array:
struct hello *helloptr = malloc(sizeof (struct hello) + count*(sizeof int)); helloptr->number = count;
The use is to distribute the header and data buffer conveniently with a single structure.
Addition: the difference in your version, which allocates an array of size 1, sizeof struct hello will be larger, since the array is allocated with one element instead of undefined = zero. This either takes up one piece of memory or makes size calculations more complex and a little ugly conceptually (your code is 1 array in size, when you actually mean "undefined", maybe even 0).
In OOP C ++, itβs better to just wrap a dynamically allocated buffer inside the class, this is really a βhackβ.
source share