TypedEf in the C / C language believes that everyone is already unfamiliar, this article is a introduction to the various usage of C / C language keyword TypeDef.
Typedef, as the name, for "type definition", can be interpreted as: Define a data type as an identifier, using this identifier in the program to implement the definition of the corresponding data type variable. E.g:
Typedef unsigned int uint
Int main (int Argc, char * argv [])
{
Unsigned int a; // it's ok
UINT B; // It's OK, A and B Are Ot of the Same Type (int)
//...............????????????????????????????????????????????
Return 0;
}
In the above code, A and B are the same data type (Unsigned INT) because the UINT identifier has been labeled as a unsigned int type. The above code seems simple, I believe that many readers have used this method, but this is not all TypedEf, which describes several usage of complex data types using typedef.
1, define structural types
Structural body is a more common data type, which is very wide in C / C programming. The following code is an application of the structural type:
#include
Int main (int Argc, char * argv [])
{
Struct {int x; int y;} Point_a, Point_b;
Point_a.x = 10; point_a.y = 10;
Point_b.x = 0; point_b.y = 0;
ios :: sync_with_stdio ();
Cout << Point_a.x point_a.y << endl;
COUT << Point_b.x point_b.y << endl;
Return 0;
}
The above code contains two structural variables: Point_a and Point_b, which are the same as the data type, all of;} types. This kind of saying may be a little awkward. It is used to saying that Point_a and Point_B are structural types, why is it necessary to say Struct {INT X; int y;} type? Because this statement is more accurate. For example, in the first example, for "Unsigned Int A, B;" statement, we can say that A and B are integer types, but more precisely, they should be the unsigned int type.
Since Struct {Int x; int y;} is a custom complex data type, then how should we write code if we want to define multiple struct {int x; int y;} type variables? In fact, it is very simple, just when struct {Int x; int y;} is a simple data type:
Struct {Int x; int y;} var_1; // Define variable var_1
Struct {Int x; int y;} Array_1 [10]; // Define array array_1
Struct {structure {int x; int y;} part; int part2;} CPLX;
The third line above defines a CPLX variable, and its data type is a complex structural type with two members: Part1 and Part2. Part1 is struct {int x; int y;} type, Part2 is int type. As can be seen from the above example, if you need multiple definitions of Struct {INT X; int y;} type in the program, you must enter "struct {INT X; int y;}" multiple times, Moreover, if a member in the structure is Struct {Int x; int y;}, it will make the definition become very complicated and easy to errors. In order to enter the program, in order to enhance the readability of the program, we can define the data type "Point" as the identifier "Point", then the above program will become more easily understood. :
Typedef struct {Int x; int y;
Point var_1; // Define Variable VAR_1
Point Array_1 [10]; // Defines an array array_1
Struct {Point Part1; INT Part2;} CPLX; // Defines complex type variable CPLX
It should be noted that we can also use the following method to define structural variables:
Struct t_point {
INT X; int y;}; // Note that the last semicolon here cannot be omitted
Int main (int Argc, char * argv [])
{
Struct t_point a, b;
//.
Return 0;
}
Obviously, this method does not have TypedEf more intuitive (in C , the struct keyword in the main line of the main function can be omitted, but in standard C, the keyword will be compiled).
Also, for the nodes in the link queue, we can implement this:
Typedef struct t_node {
Int value;
Struct t_node * next;
} Node;
Of course, you can define this:
Typedef strcut t_node node;
Struct t_node {
Int value;
Node * Next;
}
2. Define an array type
Similar to defined structural types, you can use TyPedef to define an array type, for example:
Typedef Int MyIntarray [100];
In the program
MyintArray IA;
Be equivalent to
INT IA [100];
3, define function pointer
Look at the following code:
Typedef void (* funcaddr) (int)
Here FuncAddr is a pointer to such a function, the return value of the function is a Void type, and the function has an INT type parameter. Another example:
Void Print (INT X)
{
Printf ("% d / n", x);
}
Int main (int Argc, char * argv [])
{
Funcaddr pfunc;
PFUNC = Print; // points the pointer to the Print function
(* pfunc) (25); // Call function Print
Return 0;
}
The function pointer is generally used for the callback function, the declaration of the interrupt process, and the declaration of the event processing process in object-oriented programming.
4, define class types
Class is a new data type introduced in an object-oriented programming language. Since it is a data type, you can define it using TypeDef:
Typedef class {
Private:
Int a; public:
INT B;
} Myclass;
In fact, this is very similar to the defined structure type, but few people use it.