You will find a lot of explanations if you search online, and you will always find what you need This article seems quite easy to understand. Please take a look at the detailed explanation and usage summary of the structure definition typedef struct The following is the article Text:
Typedef means type definition. typedef struct is for the convenience of using this structure.
The specific difference is: If struct node{ } defines the structure like this. When defining the structure variable of node, you need to write like this: struct node n; If you use typedef, you can write like this: typedef struct node{}NODE;. When applying for variables, you can write like this: NODE n; in fact, it is equivalent to NODE being an alias of node. The difference lies in whether the struct keyword can be omitted when using it.
1 First of all: If you want to use typedef when defining a structure type in C: typedef struct Student { int no; char name[12]; }Stu,student; So when declaring a variable, you can: Stu stu1; or: student stu2; (Stu and student are both aliases for Student) If there is no typedef: struct Student { int no; char name[12]; }Stu; You must use struct Student stu1; or struct Stu stu1; to declare and you don’t need to write it here. Student (so struct Student stu1; cannot be used anymore) typedef struct { int no; char name[12]; }Stu;
2 Secondly: If you use typedef in c++, it will make a difference: struct Student { int no; char name[12]; } stu1;//stu1 is a variable
typedef struct Student2 { int no; char name[12]; }stu2;//stu2 is a structure type, that is, stu2 is an alias of Student2 Use You can directly access stu1.no , but stu2 must first define stu2 s2; and then s2.no=10;
Every structural type (assumed to be named a) has such a pointer type (assumed to be named b) inside, that is, b is a pointer type used to store the address of a structure variable of the same type as a (as shown in Figure 1.2, the structure Point A's pointer next, node B and node A pointed to by next belong to the same structural type), then the name of the structural type a must be added after the typedef struct statement that defines a. Comparing it with the previously defined structural type TypeA, you will find the difference in the definition method of the structural type Node here.
To put it simply, we usually use different names, such as
When defining a variable, if you use
node_t
, you need to write it like this:If you use
Node
, you can write it in this simplified way:If you omit
node_t
when writing it, you cannot use the first way of writing.Generally
node_t
is used to define linked list structuresActually, this is just a combination of
struct
andtypedef
, which can be split into:You will find a lot of explanations if you search online, and you will always find what you need
This article seems quite easy to understand. Please take a look at the detailed explanation and usage summary of the structure definition typedef struct
The following is the article Text:
Typedef means type definition. typedef struct is for the convenience of using this structure.
The specific difference is:
If struct node{ } defines the structure like this. When defining the structure variable of node, you need to write like this: struct node n;
If you use typedef, you can write like this: typedef struct node{}NODE;. When applying for variables, you can write like this: NODE n; in fact, it is equivalent to NODE being an alias of node. The difference lies in whether the struct keyword can be omitted when using it.
1 First of all:
If you want to use typedef when defining a structure type in C:
typedef struct Student
{
int no;
char name[12];
}Stu,student;
So when declaring a variable, you can: Stu stu1; or: student stu2; (Stu and student are both aliases for Student)
If there is no typedef:
struct Student
{
int no;
char name[12];
}Stu;
You must use struct Student stu1; or struct Stu stu1; to declare
and you don’t need to write it here. Student (so struct Student stu1; cannot be used anymore)
typedef struct
{
int no;
char name[12];
}Stu;
2 Secondly:
If you use typedef in c++, it will make a difference:
struct Student
{
int no;
char name[12];
} stu1;//stu1 is a variable
typedef struct Student2
{
int no;
char name[12];
}stu2;//stu2 is a structure type, that is, stu2 is an alias of Student2
Use You can directly access stu1.no
, but stu2 must first define stu2 s2;
and then s2.no=10;
There is a difference. If there is a member of struct node type in the structure, then the second type will report an error
I have encountered your problem before:
Every structural type (assumed to be named a) has such a pointer type (assumed to be named b) inside, that is, b is a pointer type used to store the address of a structure variable of the same type as a (as shown in Figure 1.2, the structure Point A's pointer next, node B and node A pointed to by next belong to the same structural type), then the name of the structural type a must be added after the typedef struct statement that defines a. Comparing it with the previously defined structural type TypeA, you will find the difference in the definition method of the structural type Node here.