Struct and typedef struct are clear, and structtypedef is clear.
It can be divided into three parts:
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; (if there is no typedef, you must use struct Student stu1; to declare it)
The Stu here is actually the alias of struct Student. Stu = struct Student
In addition, Student can also be left empty here (so it cannot be struct Student stu1; it must be Stu stu1 ;)
Typedef struct
{
Int;
} Stu;
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;
========================================================== ========================================================== ======
3. You can master the above two items, but at last we will discuss the issues that do not matter much.
If we write:
Typedef struct
{
Int num;
Int age;
} Aaa, bbb, ccc;
What is this?
I personally observe the understanding of the compiler (VC6), which is equivalent
Typedef struct
{
Int num;
Int age;
} Aaa;
Typedef aaa bbb;
Typedef aaa ccc;
That is to say, aaa, bbb, and ccc are all struct types. When declaring a variable, you can use any one. This is also true in c ++. However, note that if the typedef keyword is written in c ++, aaa, bbb, and ccc will be completely different objects.
// This is not very understandable.
Differences between typedef struct and struct:
Typedef struct tagMyStruct
{
Int iNum;
Long lLength;
} MyStruct;
The above tagMyStruct is the identifier, and MyStruct is the variable type (equivalent to (int, char, etc )).
This statement actually completes two operations:
1) define a new structure type
Struct tagMyStruct
{
Int iNum;
Long lLength;
};
Analysis: tagMyStruct is called "tag", that is, "tag". It is actually a temporary name. Whether or not the typedefstruct keyword and tagMyStruct constitute this structure type, this structure exists.
We can use struct tagMyStruct varName to define variables, but note that it is incorrect to use tagMyStruct varName to define variables, Because struct and tagMyStruct can be combined to represent a structure type.
2) typedef is named MyStruct for the new structure.
Typedef struct tagMyStruct MyStruct;
Therefore, MyStruct is actually equivalent to struct tagMyStruct. We can use MyStruct varName to define variables.
2.
Typedef struct tagMyStruct
{
Int iNum;
Long lLength;
} MyStruct;
In C, there are two methods to apply for Structure Variables after this declaration:
(1) variable name of struct tagMyStruct
(2) MyStruct variable name
In c ++
(1) variable name of struct tagMyStruct
(2) MyStruct variable name
(3) tagMyStruct variable name