This question arises from the question Is a structure {...}; type or unnamed variable?
In this question, OP asked about
typedef struct student_s { char* name; int age; double height; struct student_s* next; };
I ask about the legality of the above. Is diagnostics required if the code containing above is compiled (or to simplify it, typedef struct foo {int bar;}; )?
I believe it is legal that diagnosis is not required from the point of view of a language attorney. (In addition: I am not a proponent of using this. He is very worthy of diagnosis. I would very much like the compiler to warn me if I mistakenly wrote the code as indicated above.)
Section 6.7 of the C11 standard dictates the syntax of declarations: declare-specificers init-declarator-list opt ; Please note that the init-declarator list is optional. This may make typedef int; valid. This is not because the standard also states that
A declaration other than a static_assert declaration must declare at least a declarator (except for function parameters or members of a structure or union), a tag, or enumeration members.
Thus, typedef int; and typedef struct {int bar}; are illegal because they do not declare a declarator, tag, or listing member.
On the other hand, it seems to me that typedef struct foo {int bar;}; is legal because he announces something. In particular, it declares and defines a struct foo tag.
Is the above reasoning correct?
source share