Typedef is a keyword in C language. Its function is to define a new name for a data type. The data types here include internal data types [int, char, etc.] and custom data types [stuct, etc.] .
Typedef is a common syntax in C/C. The function of typedef can be summed up into four types:
1. Define a type Name
In conventional applications, if you want to define two character pointers, write the following code: char *a, b
(1) char* a,b;
(2) char c='m';
(3) a=&c;
(4) b=&c;
The above code is wrong, only a is a character pointer, and b is still a character variable. Macro definition through #define is still invalid, because macro definition is only character replacement.
The following is feasible:
(1)typedef char* PCHAR;
(2)PCHAR pa, pb;
2. Cross-platform transplantation
When writing programs, if you take into account platform porting factors, you need to abstract the differences in the hardware layer from the code, such as the space occupied by variables, end mode, etc.
Consider a floating-point variable. On different hardware platforms, the space it occupies may be different. In this case, you can use typedef to define it in a separate header file. This header file is purely abstract. Hardware related content:
(1) typedef float REAL;
(2) typedef short int INT16;
(3) typedef int INT32
(4)...
In this case, if you consider transplanting the program in the future, you only need to modify the header file.
3. Give aliases to complex declarations
Complex declarations are in the form: void (*b[10]) (void (*)());
Meaning : First, *b[10] is a pointer array, and the ten elements in it are all pointers. What kind of pointer is it? It is a function pointer with a null return type and null formal parameters.
This kind of complex declaration can be simplified with typedef:
First: declare the function pointer behind:
(1) typedef void (pFunParam *)();
Then declare the previous pointer array:
(1) typedef void (*pFunx)(pFunParam);
The simplified version of the original declaration:
pFunx b[10];
During the writing process of this document, I referred to the online blog typedef usage
which mentioned a complex statement:
(1) doube (*)() (*e)[9];
However, this statement does not pass when compiled under gcc. According to the author's original intention, it seems that it should be declared like this:
(1 )double (*(*e)[9])();
e is a pointer to a 9-dimensional array. The array is a function pointer. The formal parameter of the function pointer is empty and the return type is double.
In this case, such typedef should be used to simplify the declaration:
typedef (*(*ptr)[9])();
Recommended tutorial: "c language tutorial》
The above is the detailed content of What does typedef mean in c language. For more information, please follow other related articles on the PHP Chinese website!