Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Legality of `typedef struct foo {int bar};`

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?

like image 470
David Hammen Avatar asked Aug 07 '15 13:08

David Hammen


People also ask

What is difference between struct and typedef struct?

In C++, there is no difference between 'struct' and 'typedef struct' because, in C++, all struct/union/enum/class declarations act like they are implicitly typedef'ed, as long as the name is not hidden by another declaration with the same name.

What is struct and typedef?

The C language contains the typedef keyword to allow users to provide alternative names for the primitive (e.g.,​ int) and user-defined​ (e.g struct) data types. Remember, this keyword adds a new name for some existing data type but does not create a new type.

Should you typedef structs?

In general, a pointer, or a struct that has elements that can reasonably be directly accessed should never be a typedef. 'Opaqueness and "accessor functions" are not good in themselves'.

What is the use of typedef in C++?

typedef is a reserved keyword in the programming languages C and C++. It is used to create an additional name (alias) for another data type, but does not create a new type, except in the obscure case of a qualified typedef of an array type where the typedef qualifiers are transferred to the array element type.


1 Answers

In the quote you cited there is written that a declaration shall declare among other things a tag.

So this declaration

typedef struct student_s {
    char* name;
    int age;
    double height;
    struct student_s* next;   
};

simply declares struct student_s and equivalent to declaration without the typedef specifier

struct student_s {
    char* name;
    int age;
    double height;
    struct student_s* next;   
};

It does does not introduce a typedef name in the scope.

like image 184
Vlad from Moscow Avatar answered Sep 30 '22 08:09

Vlad from Moscow