Detailed description of C typedef struct
Define a struct in C to use typedef, for example:
typedef struct Student { int a;}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.
Of course, Student can not be written here, as shown below:
typedef struct { int a;}Stu;
Stu stu1 must be used to declare the object.
See the following code:
typedef struct tagMyStruct{ int iNum; long lLength;} MyStruct;
The above tagMyStruct is the identifier, and MyStruct is the variable type. The above Code actually completed two operations:
(1) define a struct:
struct tagMyStruct{ int iNum; long lLength;};
TagMyStruct is called a tag, which is actually a temporary name. Whether or not there is a typedef struct keyword or a tagMyStruct keyword, the struct is constructed. In this case, we can use struct tagMyStruct varName to define variables. However, it is incorrect to use tagMyStruct varName to define variables. The combination of struct and tagMyStruct represents a struct 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.
The same Code has different understandings in C and C ++. As follows:
typedef struct tagMyStruct{ int iNum; long lLength;} MyStruct;
In C, this struct declares struct variables in two ways:
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;