1 first: // note the differences between C and C ++
To define a struct type in C, use typedef:
Typedef struct student
{
Int;
} Stu;
So when declaring the variable, you can: Stu stu1;
The Stu here is actually the alias of struct student. Stu = struct student
However, in C ++
Struct student
{
Int;
};
As a result, student of the struct type is defined, and student stu2 is directly used when the variable is declared;
========================================================== ========================================================== ======
2. Second:
In C ++, if typedef is used, the following differences may occur:
Struct student
{
Int;
} Stu1; // stu1 is a variable.
Typedef struct student2
{
Int;
} Stu2; // stu2 is a struct type = struct student
You can directly access stu1.a during use.
But stu2 must first stu2 S2;
Then s2.a = 10;
========================================================== ========================================================== ====
Summary:
C/C ++ can all be like this:
Typedef struct student
{
Int;
} Stu;
In this case, Stu is a type and Stu stu1 can be used to declare variables;
However, C ++ is simple:
Struct student
{
Int;
};
At this time, student is a type with the declared variable: Student stu1
Remember to sum up.
Struct and typedef struct