In C language, what is the meaning of struct Animal;
in line # 6?
Is it legal in C89 or C99 or C11?
struct Animal {
char *name;
int age;
};
struct Cat {
struct Animal; // line 6
int category;
};
Thanks!
In C language, what is the meaning of struct Animal;
in line # 6?
Is it legal in C89 or C99 or C11?
struct Animal {
char *name;
int age;
};
struct Cat {
struct Animal; // line 6
int category;
};
Thanks!
It doesn't mean much of anything -- and in fact it's a constraint violation.
The language grammar allows
struct Animal;
in place of a member declaration inside a struct or union declaration. But it violates a language rule (N1570 6.7.2.1 paragraph 2):
A struct-declaration that does not declare an anonymous structure or anonymous union shall contain a struct-declarator-list.
This is a "constraint", which means that violating it requires a diagnostic. (A non-fatal warning message qualifies as a diagnostic.)
If you had written:
struct Cat {
struct Animal foo;
int category;
};
then the foo
would be a "declarator". The constraint I quoted above means you're not allowed to omit it. gcc and clang both warn about this by default ("declaration does not declare anything") and reject it with -Wpedantic-errors
.
(Anonymous structs and unions were added to the language in C11, and are discussed here, but the code in your question is not an anonymous struct.)