Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is a struct {...}; a type or an unnamed variable?

Tags:

c

struct

Is the following, at file scope, a type declaration or an unnamed variable?

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

If it is a type definition, then what is the difference with:

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

?

(Background: see my answer at Changing a variable from global to local - C, where I believe the first introduces an unnamed variable that is then optimized away by the compiler.)

Note: the question has been flagged as a possible duplicate of In what scope is a struct member identifier put? but I believe I am not asking a question about the scope of the members but about what the declarations actually create. However. answers to Difference between 'struct' and 'typedef struct' in C++? do explain my question.

like image 977
Paul Ogilvie Avatar asked Aug 07 '15 12:08

Paul Ogilvie


2 Answers

As per the C standard, a structure declaration in the form like

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

is the declaration of a type. Quoting C11, chapter §6.7.2.1

The presence of a struct-declaration-list in a struct-or-union-specifier declares a new type, within a translation unit. The struct-declaration-list is a sequence of declarations for the members of the structure or union. [...]

C standard does not mandate creating a variable (either named or unnamed) for that type.

In your second snippet, you actually (try to) typedef to nothing. However, if you change your snippet to the form of

typedef struct {
         //.....members
} student_s ;

you'll be creating a type student_s (typedef to an unnamed structure type) which you can use later.

FWIW, we never talked about a variable being created here, anyways. It's all about types.

like image 175
Sourav Ghosh Avatar answered Sep 22 '22 11:09

Sourav Ghosh


First declaration declares a type. After that declaration, the type struct student_s is known, and you can declare variables of that type or pointer to it :

struct student_s student1;
struct student_s *pStudent;

The second declaration is weird even if is compiles. The common usage would be :

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

which declares an alias student_t to an anonymous struct. It can then be used directly :

student_t student1;
student_t *pStudent;

BUT THE SECOND EXAMPLE DOES COMPILE (even with a warning) AND IS THE SAME AS FIRST ONE !

What it really does is to declare a void alias to struct student_s. The typedef is ignored and produces a warning : typedef requires a name, but as a side effect, the struct is declared, exactly as it was in first example.

So the true answer to the actual question is THERE ARE NO DIFFERENCES except for a warning.

like image 40
Serge Ballesta Avatar answered Sep 18 '22 11:09

Serge Ballesta