typedef

 Common usage of typedef

1. Conventional variable type definition
such as: typedef unsigned char uchar
description: uchar is equivalent to unsigned char type definition uchar c declaration is equal to unsigned char c declaration
2. Array type definition
such as: typedef int array[2];
description: array is equivalent to int[2] definition; array a declaration is equivalent to int a[2] declaration
Extension : typedef int array[M][N];
description: array is equivalent to int [M][N] definition; array a declaration is equivalent In int a[M][N] declaration
3. Pointer type definition
such as: typedef int *pointer;
description: pointer is equivalent to int * definition; pointer p declaration is equivalent to int *a declaration
For example : typedef int *pointer[M ];
Description: pointer is equivalent to int *[M] definition pointer p declaration is equivalent to int *a[M] declaration
4. Function address description
Description: C treats the function name as the first address of the function, we can use The easiest way to get the function address
For example : Function:int func(void); unsigned long funcAddr=(unsigned long)func, the value of funcAddr is the first address of the func function
5. Function declaration
For example: typedef int func(void); func is equivalent to a function of type int (void)
Description 1: func f declaration is equivalent to int f(void) declaration, for file function declarations
Description 2: func *pf declaration is equivalent In the declaration of int (*pf)(void), it is used for the life of function pointer, see the next item
6. Function pointer
example: typedef int (*func)(void)
description: func is equivalent to int (*)(void) type
func pf is equivalent to int (*pf) (void) declaration, pf is a function pointer variable
7. Identify the method of typedef:
a). The first step. Substitute the name after typdef with a known type definition until only one name is left that is not recognized as correct.
Such as typedef u32 (*func)(u8);
find typedef __u32 u32 from the above definition; typedef __u8 u8
continue to find typedef unsigned int __u32; typedef unsigned char __u8;
alternate location name typedef unsigned int (*func)(void);
now only func is unknown.
b). The second step. The unknown name is the defined type, and the type is to take out all parts of the name and typedef. As above,
func is equivalent to unsigned unsigned int (*)(unsigned char);
c).Part 3. When defining a variable, the variable type is equivalent to the type obtained by substituting the variable for the position of the unknown name
func f is equivalent to unsigned unsigned int (*f)(unsigned char)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324590671&siteId=291194637