In C ++, all members of a user type must have full types, and the arr member does not have a full type unless you give it a size.
In C, the structure definition will be compiled, but you may not get what you want. The problem is that an array without size is allowed at the end of the structure, which will be used as a proxy to access the contiguous block of memory after the instance. This allows you to implement a mute vector:
typedef struct vector { int size; char buffer[]; } vector; vector* create_vector( int size ) { vector* p = (vector*) malloc( sizeof *p + size );
But the language does not allow you to initialize curly braces, since the compiler does not know how much memory needs to be held (in general, in some cases it can know). A member of a structure that does not have a size is only a way of accessing outside the object; it does not save memory by itself:
printf( "sizeof(vector)=%d\n", sizeof(vector) );
source share