Is a structure {...}; type or unnamed variable?

Is there a type declaration in the file scope or an undefined variable?

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

If this is a type definition, then what is the difference:

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

?

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

Note : the question was marked as a possible duplicate. In what area is the identifier of the structure element present? , but I believe that I am not asking a question about the scope of the members, but about what declarations really do. But. answers to the Difference between 'struct' and 'typedef struct' in C ++? explain my question.

+6
source share
6 answers

In accordance with standard C declaration in the form of a type

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

- type declaration. Citation C11 , chapter ยง6.7.2.1

Having a struct-declaration-list in a struct-or-union-specifier declares a new type inside a translation unit. A struct-declaration-list is a sequence of declarations for members of a structure or union. [...]

Standard

C does not provide for creating a variable (either named or unnamed) for this type.

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

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

you will create a student_s type ( typedef for an unnamed structure type), which you can use later.

FWIW, we never talked about the variable being created here, anyway. All about types.

+6
source

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

 struct student_s student1; struct student_s *pStudent; 

The second declaration is strange, even if compiled. General use:

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

which declares the alias student_t an anonymous structure. Then it can be used directly:

 student_t student1; student_t *pStudent; 

BUT THE SECOND EXAMPLE IS COMPOSITION (even with a warning) AND SO FIRST!

Actually, this means declaring an alias to void struct student_s . Typedef is ignored and gives a warning: typedef requires a name, but as a side effect, the structure is declared in the same way as in the first example.

Thus, the true answer to the actual question DOES NOT PERFECT except for the warning.

+3
source

Both are type definitions. The second one is incomplete. It does not give a name as an argument to typedef . Much better to use

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


Regarding legality

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

I asked this as a separate question, Legality` typedef struct foo {int bar}; `

+2
source

This is a type, and you cannot have an anonymous instance of the structure. For comparison, this declares a struct_type type and an instance of a struct_instance this type:

 struct struct_type { /* blah */ } struct_instance; 

If you want to declare another instance separately (from decl type), you should use

 struct struct_type another_instance; 

Using typedef โ€” if done correctly, unlike your example โ€” simply allows you to specify the type of another name that does not require the struct keyword to declare an instance:

 typedef struct_type MyStruct; MyStruct yet_another_instance; 

or equivalent

 typedef struct struct_type { /* blah */ } MyStruct; 

omitting the name ( struct_type ) gives you an anonymous type of structure, which can only be referenced by its name typedef'd.


Note 1

Since your source structure contains a next pointer for its own type, this type must have a name at the point declared by the member. Thus, you cannot declare an anonymous structure using a typical pointer. If you give your anonymous structure type a name with typedef , that name does not exist until a member declaration is declared, so it cannot be used.

 typedef struct /*S*/ { struct S *next; /* T doesn't exist yet, so without S, you can't declare this */ } T; 

Note 2

You can declare an anonymous instance of an anonymous union as a member:

 struct S { union { int i; unsigned u; }; }; struct S s; si = -1; printf("%x\n", su); 

but this is a very special case. I made a remark about this from the main argument, if it is misleading.

+1
source

The difference is that in the first example you need to declare a new variable as follows: struct student_s variable; , however with the second example you can just make student_s variable; .

0
source
 struct A { ... } 

creates a struct 'A' visible in the struct namespace (which has nothing to do with C ++ namespaces). So, to access struct you need to use the struct keyword;

 struct A a; 

When defined using typedef struct

 typedef struct A { ... } B; 

becomes visible and attached to B, and you can easily create a struct as a generic variable of type B

 B a; 

Please correct me if I am wrong.

0
source

All Articles