Your colleagues lied. (Probably not intentionally, although do not be angry with them or anything else.)
This is called the flexible element of the array, and is written as char bar[]; in C99 char bar[]; , and in C89 - as a char bar[1]; , and some compilers allow you to write as char bar[0]; . Basically, you only use structure pointers and allocate them all with extra space at the end:
const size_t i = sizeof("Hello, world!"); struct foo *p = malloc(offsetof(struct foo, bar) + i); memcpy(p->bar, "Hello, world!", i);
Thus, p->bar stores a string whose size is not limited by the declaration of the array, but is still executed in the same distribution as the rest of the struct (instead of the member being char * and installing it requires two malloc and two free )
Chris lutz
source share