TypedefIs used to declare custom data types, with a variety of original data types to simplify programming PurposeType Definition keyword.
Method for defining types using typedef
① Write the definition body (for example, int I) according to the definition variable method ).
② Replace the variable name with the new type name (for example, replace I with count ).
③ Add typedef. (For example, typedef int count) at the beginning ).
④ Then you can define variables with the new type name.
Note:
(1) typedef can be used to declare various types of names, but cannot be used to define variables.
(2) Using typedef only adds a type name to an existing type without creating a new type.
(3) When the same type of data is used in different source files, typedef is often used to declare some data types and put them in a single file, then, use the # include command to include them in the files that require them.
(4) The use of typedef facilitates the general application and Transplantation of programs.
(5) typedef is similar to # define. For example, typedef int count; # define count int indicates Int. But in fact, they are different.. # Define is used during pre-compilation.
, It can only be replaced with a simple string, AndTypedef is processed during compilation.. In fact, it is not a simple string replacement,Declare a type as if the variable is defined.
Struct and typedef
Struct {
Int X;
Int y;
} Test1;
The test1, test1.x, and test1.y struct are defined and can be used in the statement.
Struct test {
Int X;
Int y;
} Test1;
The test1, test1.x, and test1.y struct are defined and can be used in the statement. Compared with the previous one, test is saved.
Typedef struct test {
Int X;
Int y;
} Text1, text2;
The struct alias is text1 or text2. Actually used in the statement, but also to write:Text1 Te;
Then you can useTe. x Te. Y
Typedef struct {
Int X;
Int y;
} Test1;
Same as the preceding one, you must also write:Test1 my_st;Available
My_st.xAndMy_st.y