The usage of typedef

  • 2020-04-02 01:03:14
  • OfStack

This keyword may not be used much by novices, but it is a useful keyword for modularity (i.e. less association with other code), the basis for implementing Traits in C++, and one of the basic syntax of template programming.

If a variable definition is to name a variable, a typedef (or type definition) is to name a type. Since they're both named, there's a lot of similarities. And the variable definition I think you all use, so the type definition is bound to use.

The syntax for a type definition can be boiled down to this: it becomes a type definition as long as a variable definition is preceded by a typedef. What's supposed to be a variable here becomes a type.
For example, the following variable definition:
Int the integer;         // integer variable
Int * pointer;     // integer pointer variable
An int array [5]; // integer array variables
Int * p_array [5]; // integer pointer to the array of variables
Int (* array_pointer) [5]; // integer array pointer to the variable
The int function (int param); // function definition, you can also think of function names as function variables
Int * function (int param); // is still a function, but the return value is an integer pointer
Int (* function) (int param); // is now a pointer to the function
To define the corresponding type, name the type as follows:
Typedef int integer_t;                                           // integer type
Typedef int * pointer_t;         // integer pointer type
Typedef int array_t [5]. // integer array type
Typedef int * p_array_t [5];       // the type of array of integer Pointers
Typedef int (* array_pointer_t) [5]; // the type of pointer to an integer array
Typedef f int function_t (int param);         // function type
Function_t (int param);       // function type
Typedef int (*function_t) (int param); // the type of pointer to the function
Note: The above function type may be wrong in C, because there is no function type in C, its function variables will automatically degenerate into function Pointers; It seems to work in C++. The main point here is the formal similarity.
 
The general form of typedef is:
typedef     type         Definition name;
Generally, typedefs are used in programming for two purposes: one is to give a variable a new name that is easy to remember and has a clear meaning; the other is to simplify some complex type declarations.
In fact, when variables are declared in C, there is a storage-class-specifier, which includes the familiar extern, static, auto, register. When a storage type indicator is not specified, the compiler automatically takes the default value by convention. In addition, the location of the storage type indicator is also arbitrary (but before the variable name and pointer *), which means that the following lines of code are equivalent:
The static const int I;
Const static int I;
Int const static I;
Const int the static I;
According to the C language specification, In syntactic analysis, typedefs and storage type indicators are equivalent ! Therefore, we replace the above static place with a typedef:
Typedef const int I;
Const typedef int I;
Int const typedef I;
Const int typedef I;
The semantics of the above code are: define I as a type name whose equivalent type is const int. In the future if we have I     Code a is equivalent to const int a. The same is true for places with Pointers, such as:
Int const typedef * t; So code t     P. Int const *p.
In addition, a typedef cannot be used at the same time as a storage type indicator such as static, because each variable can only have one storage type, so the code is: typedef static int I; It's illegal.
Use a typedef to simplify complex variable declarations
1) define an array with 10 Pointers to a function that takes an integer parameter and returns an integer?
The first method: int (*a[10])(int);
The second method is typedef int (*pfunc)(int).
                        Pfunc a, [10].
2) define an array with 10 Pointers to a function that takes a pointer to a function (no arguments, return null) and returns null.
The first method: *a[10])(void (*)(void));
The second method: typedef void (*pfuncParam)(void);
                            Typedef void (* pfunc) (pfuncParam);
Pfunc a, [10].
3) a pointer to an array with 10 function Pointers (no arguments, return value double)
The first method: double (*)(void) (*p)[10];
Second method: typedef double (*pfunc)(void);
                        Typedef pfunc (* pfuncParam) [10];
                        PfuncParam p;
Conclusion:
Typedef can be used in two ways:
In general, you define an alias of an existing type
typedef     type       Definition name;
Create a new type
        typedef     Return value type     New type name (parameter list);

Related articles: