This question arises from the question Is a struct {...}; a type or an unnamed variable?
In that question, the OP asked about
typedef struct student_s {
char* name;
int age;
double height;
struct student_s* next;
};
I am asking about the legality of the above. Is a diagnostic required when code containing the above is compiled (or to make it simpler, typedef struct foo {int bar;};
)?
My take is that it is legal, that a diagnostic is not required from a language lawyer perspective. (Aside: I am not advocating using this. It is very worthy of a diagnostic. I would very much want the compiler to warn me if I mistakenly wrote code like the above.)
Section 6.7 of the C11 standard dictates the syntax of a declaration: declaration-specifiers init-declarator-listopt ;
Note that the init-declarator-list is optional. This might lead one to think that typedef int;
is valid. It isn't because the standard also says that
A declaration other than a static_assert declaration shall declare at least a declarator (other than the parameters of a function or the members of a structure or union), a tag, or the members of an enumeration.
Thus typedef int;
and typedef struct {int bar};
are illegal because they do not declare a declarator, a tag, or a member of an enumeration.
On the other hand, it appears to me that typedef struct foo {int bar;};
is legal because it does declare something. In particular, it declares and defines the struct
tag foo
.
Is the above reasoning correct?