Typedef is a type definition keyword that is used in computer programming languages ​​to declare custom data types and to match a variety of legacy data types to simplify programming. This article mainly introduces the uses and traps of Typedef for reference.
The use of typedef in programming is generally two purposes, one is to give the variable a new name that is easy to remember and has a clear meaning, and the other is to simplify some of the more complex type declarations. Let's take a look at the use of typedefs and the pitfalls.
Define a type of alias, not just a simple macro replacement. Can be used as multiple objects that declare pointers at the same time. such as:
Char* pa, pb; // This majority does not meet our intentions, it only declares a pointer to a character variable and a character variable
The following is feasible:
Typedef char* PCHAR; // Normally capitalized
PCHAR pa, pb; // Feasible, while declaring two pointers to character variables
although:
Char *pa, *pb;
It's also possible, but it's relatively unintuitive in terms of typedef, especially in places where a lot of pointers are needed.
Use two:Used in the old C code (more specific old no check), help struct. In the previous code, when declaring a struct new object, you must bring a struct, ie, the form: struct structure name object name, such as:
Struct tagPOINT1
{
Int x;
Int y;
};
Struct tagPOINT1 p1;
In C + +, you can directly write: structure name object name, namely:
tagPOINT1 p1;
It is estimated that someone thinks that it is too much trouble to write more than one struct, so he invented:
Typedef struct tagPOINT
{
Int x;
Int y;
}POINT;
POINT p1; // This writes a struct less than the original one, which saves time, especially when used in large quantities.
Perhaps, in C + +, the use of typedef is not very big, but understand it, it is still helpful to master the old code, after all, we may encounter in the project legacy code from the earlier time .
Use three:Use typedef to define platform-independent types.
For example, to define a floating-point type called REAL, on the target platform, let it represent the most precise type:
Typedef long double REAL;
On platform two that does not support long double, change to:
Typedef double REAL;
On platforms three that do not support double, change to:
Typedef float REAL;
In other words, when cross-platform, just change the typedef itself, without any changes to other source code.
The standard library uses this technique extensively, such as size_t.
In addition, because typedef is a new alias that defines a type, not a simple string replacement, it is more robust than macros (although macros can sometimes do the same thing).
Use four:Define a new simple alias for a complex statement. The method is to replace some of the complex declarations gradually with the alias in the original declaration. In this way, the part with the variable name is left to be replaced at the end, and the most simplified version of the original statement is obtained. For example:
1. Original statement:Int *(*a[5])(int, char*);
Variable named a, directly replace a with a new alias pFun on it:
Typedef int *(*pFun)(int, char*);
The original simplified version of the statement:
pFun a[5];
2. Original statement:Void (*b[10]) (void (*)());
Variable name is b, replace the right parenthesis, pFunParam is an alias:
Typedef void (*pFunParam)();
Then replace the variable b on the left, pFunx is alias two:
Typedef void (*pFunx)(pFunParam);
The original simplified version of the statement:
pFunx b[10];
3. Original statement:Doube(*)() (*e)[9];
Variable named e, first replace the left part, pFuny alias one:
Typedef double(*pFuny)();
Then replace the right variable e, pFunParamy alias two
Typedef pFuny (*pFunParamy)[9];
The original simplified version of the statement:
pFunParamy e;
Understand the "right-left rule" available for complex declarations: Look from the variable name, first to the right, then to the left, touch a parenthesis to read the direction of reading; after parsing inside the parentheses, or out of the right and left The order, so loop, until the entire statement is analyzed. For example:
Int (*func)(int *p);
First find the variable name func, outside a pair of parentheses, and the left is an asterisk, which means that func is a pointer; then jump out of the parentheses, first look at the right, and then encounter parentheses, which means (*func) is a Function, so func is a pointer to such a function, that is, a function pointer, such a function has an int* type parameter, and the return value type is int.
Int (*func[5])(int *);
The right side of func is a [] operator, indicating that func is an array of 5 elements; there is a * on the left of func, indicating that the func element is a pointer (note that the * here is not a func, but a func[5]. The reason is that the [] operator has a higher precedence than *, and func combines with [] first. Jump out of this bracket, look at the right, and then encounter parentheses, indicating that the elements of the func array are pointers to function types, and the functions it points to have int* type parameters, and the return type is int.
You can also remember 2 modes:
Type (*)(....) function pointer
Type (*)[] array pointer
Trap one:Remember, typedef is a new alias that defines a type. Unlike macros, it is not a simple string replacement. such as:
First define:
Typedef char* PSTR;
then:
Int mystrcmp(const PSTR, const PSTR);
Is const PSTR actually equivalent to const char*? No, it is actually equivalent to char* const.
The reason is that const gives the entire pointer itself a constant, which is the constant pointer char* const.
In simple terms, remember that when const and typedef appear together, the typedef will not be a simple string replacement.
Suizhou simi intelligent technology development co., LTD , https://www.msmvape.com