F
Foo
Hello group. I don't know if the subject was already debated, but I
assumed C99 allowed variably modified arrays in structs and used them in
a project. GCC didn't complain, until I upgraded to a more recent
version (4.3.3 if it matters) which now outputs the following warning:
warning: a member of a structure or union cannot have a variably
modified type
The standard doesn't seem to mention them either so I guess I was wrong.
Anyway, I find those very useful when combined with sizeof to make the
code clearer, for example:
{
size_t a;
size_t b;
void *c;
/* [...] code that computes a and b */
{
struct {
char e[a];
int f;
} *d;
d = malloc(sizeof(*d));
/* [...] code that copies data into d */
d->f[0] = 42;
c = d;
}
}
Since C99 already allows variably modified arrays and using sizeof on
them, what technical reason prevents this syntactic sugar to work with
structs?
assumed C99 allowed variably modified arrays in structs and used them in
a project. GCC didn't complain, until I upgraded to a more recent
version (4.3.3 if it matters) which now outputs the following warning:
warning: a member of a structure or union cannot have a variably
modified type
The standard doesn't seem to mention them either so I guess I was wrong.
Anyway, I find those very useful when combined with sizeof to make the
code clearer, for example:
{
size_t a;
size_t b;
void *c;
/* [...] code that computes a and b */
{
struct {
char e[a];
int f;
} *d;
d = malloc(sizeof(*d));
/* [...] code that copies data into d */
d->f[0] = 42;
c = d;
}
}
Since C99 already allows variably modified arrays and using sizeof on
them, what technical reason prevents this syntactic sugar to work with
structs?