Original: http://www.nowamagic.net/librarys/veda/detail/1785
A typedef is a C-language keyword that defines a new name for a data type. The data types here include the internal data type (INT,CHAR, etc.) and the custom data type (struct, etc.).
The purpose of using TypeDef in programming is generally two, one is to give the variable a new name that is easy to remember and meaning, and the other is to simplify some more complex type declarations.
As to what is so subtle about TypeDef, look at the specifics of some of the issues that confront you.
One
First define a struct type in C to use a typedef:
struct student{ int A;} Stu;
So when declaring variables, you can: Stu stu1;
If there is no TypeDef, it must be declared with a struct Student stu1.
The Stu here is actually the alias of the struct student.
In addition here can also not write Student (so also can't struct Student stu1;).
struct{ int A;} Stu;
But in C + + It's simple, directly
struct student{ int A;};
The struct type student is defined and the variable is declared directly student stu2.
Two
Secondly, if you use typedef in C + +, it will make a difference:
struct Student { int A; } STU1; //structint//
The stu1.a can be accessed directly when used, but the STU2 must be STU2 S2 first, and then s2.a=10;
Three
Master the above two, but in the end we'll talk about a problem that doesn't matter much.
If in the C program we write:
struct { int num; int age ;} AAA,BBB,CCC;
What the hell is this?
I personally observe the understanding of the compiler (VC6), which is equivalent to
struct { int num; int age ;} Aaa;typedef AAA bbb;typedef AAA CCC;
That is to say, AAA,BBB,CCC are all structural types. Any one can be used to declare a variable, as is the case in C + +. But what you should note is that in C + + if the typedef keyword is written out, then AAA,BBB,CCC will be a distinct three object.
struct and typedef in C language struct< reprint >