What does the following mean?
struct foo
{
...
char bar[0]; // Zero size???
};
I asked my colleagues and they told me it\'s the same as
Chris' answer is correct, but I'd probably allocate the object slightly differently.
int n = ...; // number of elements you want
struct foo *p = malloc(offsetof(struct foo, bar[n]));
then iterate over it with
for (int i = 0; i < n; ++i) {
p->bar[i] = ...;
}
The key point is that Chris' answer works since sizeof(char)==1
, but for another type, you'd have to explicitly multiply by sizeof *bar
.