What exactly does typedef struct node {...} Node do; represent?

Please review this code block:

typedef struct node { int data; struct node *next; } Node; 

In this code, is Node synonymous with struct node , as defined by typedef, or is Node synonym for structure? If the latter is the case, then struct node *next; equivalent to struct struct *next; ?

Am I cheating on something?

+5
source share
5 answers

When you use typedef , you create an alias of some type.

So yes, Node is an alias for struct node .


Also, the code you have is equivalent

 struct node { int data; struct node *next; }; typedef struct node Node; 

typedef not part of the structure definition, it is part of the Node definition.

+11
source

Node is synonymous with struct node . This is why (for your example) instead of using

 struct node* p; 

Can be used

 Node* p; 
+4
source

In C grammatical structures is defined as follows:

struct-or-union-specifier:
struct-or-union identifier opt {struct-declaration-list}

So, to refer to this structure specifier, you need to use its name.

You can declare variables as follows

 struct node { int data; struct node *next; } Node; 

Here, Node is an object of type struct node . In turn, the struct node is a Node variable type specifier.

You can omit the identifier in the structure specifier. In this case, the structure is called an unnamed structure. However, using such a structure, you cannot refer to it inside its definition. For example, you cannot write

 struct { int data; struct *next; ^^^^^^^^^^^^^ } Node; 

because it is not known which structure is referenced here.

You can use unnamed structures as members of other structures. In this case, such a structure is called an anonymous structure, and its members become members of the encompassing structure.

for instance

 struct A { struct { int x; int y; ]; int z; }; 

This structure A has three members x , y and z .

When a storage class specifier is used, the declarator is an identifier of type typedef, which indicates the type specified for the identifier.

So in this ad

 typedef struct node { int data; struct node *next; } Node; 

Node no longer an object. This is a type name denoting a struct node.

So now you can use a name of type Node instead of a specifier of type struct node

+2
source

you no longer need to write struct everywhere. This not only saves keystrokes, but can also make the code cleaner as it provides a more abstraction of smidgen.

Such things as

 typedef struct { int x, y; } Point; Point point_new(int x, int y) { Point a; ax = x; ay = y; return a; } 
+1
source
 typedef struct node { int data; struct node *next; } Node; 

It can be understood simply

 struct node { int data; struct node *next; }; typedef struct node Node; 

 struct [structure tag or label] { member definition; ... member definition; } [one or more structure variables]; 

A new variable can be defined as:

 struct label <variable>; 

or if you use typedef struct label, you do not need to repeat each time to define a new iee structure variable

 typedef struct label Node; 

Node can now be used to define a new similar variable.

+1
source

Source: https://habr.com/ru/post/1237090/


All Articles