The four USES of typedefs in C language are deeply analyzed

  • 2020-04-02 01:27:22
  • OfStack

Use a:
Define a type of alias, not just a simple macro substitution. Can be used to declare multiple objects of pointer type at the same time. Such as:
Char * pa, pb; // most of this is not consistent with our intention, it only declares a pointer to a character variable, and a character variable;
The following are feasible:
Typedef char * PCHAR; // usually in capitals
PCHAR pa, pb; // works, declaring two Pointers to character variables at the same time
Although:
Char * pa * pb; It is also feasible, but it is relatively unintuitive in the form of typedefs, especially where a large number of Pointers are needed.

Use 2:
Used in the old C code (the specific old is not checked) to help struct. In the previous code, when declaring the new object of struct, it must take the struct with it, that is, the form is: 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.
Maybe someone thought it was too much trouble to write an extra struct all the time, so they invented it:

typedef struct tagPOINT
{
int x;
int y;
}POINT;
POINT p1; //This makes it easier to write one struct less than the original way, especially when used in large quantities

Perhaps this use of typedefs in C++ isn't very big, but understanding them can be helpful in mastering older code, since we might encounter code from earlier eras in our projects.

Use three:
Use typedefs to define platform-independent types.
For example, define a floating point type called REAL, and on target platform 1, let it represent the type with the highest precision as:
Typedef long double REAL;
On platform 2, which does not support long double, change to:
Typedef double REAL;
On platform 3, which doesn't even support double, change to:
Typedef float REAL;
That is, when cross-platform, as long as the typedef itself is changed, do not have to make any changes to other source code.
The standard library USES this technique extensively, such as size_t.
Also, because typedefs define a new alias of a type, rather than a simple string substitution, they are more robust than macros (although macros can sometimes do the above).

Use four:
Define a new simple alias for complex declarations. Creating a type alias for a complex variable is as simple as replacing the variable name with the type name in a traditional variable declaration expression, and then adding the keyword typedef to the beginning of the statement. The principle is to gradually replace part of the complex declaration with an alias in the original declaration, and so on in a loop, leaving the part with the variable name to replace at the end, to get the simplest version of the original declaration. For example:

Int * (*a[5]) (int, char*);
The variable is called a, just replace a with a new alias, pFun:
Typedeff int * (*pFun) (int, char*);
A simplified version of the original statement:
PFun a, [5].

Void (*b[10]) (void (*) ());
The variable is called b, first replace the parenthesis in the right part, pFunParam is alias one:
Typedef void (*pFunParam) ();
Replace the variable b on the left,pFunx is alias two:
Typedef void (*pFunx) (pFunParam);

A simplified version of the original statement:
PFunx b [10];
Understand the "right-left rule" for complex declarations: start with the variable name, go right, then left, then reverse the direction of the reading when you hit a parenthesis; When the analysis is complete, jump out of the parenthesis, again in right-to-left order, and so on, until the entire declaration is analyzed. For example:
Int (*func) (int *p);

First of all, find the variable name func, there is a pair of parentheses outside, and the left side is a *, which means that func is a pointer; I'm going to jump out of the parenthesis, and I'm going to go to the right, and I'm going to go to the parenthesis again, and that says that *func is a function, so func is a pointer to this type of function, which has an int* parameter, which returns an int.
Int (*func[5]) (int *);

To the right of func is a [] operator that indicates that func is an array of five elements. There is a * on the left of func, indicating that the element of func is a pointer (note that the * here does not modify func, but modifies func[5], because the [] operator has a higher priority than *, and func combines with [] first). Jump out of this bracket, look at the right hand side, and see the parentheses again, indicating that the element of the func array is a pointer to a function of type int*, and that the function it points to has an int* parameter, and the return value is of type int.


Related articles: