In depth analysis of C language typedef and complex function declaration

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

The following is the declaration of three variables. I want to use typedefs to define an alias for each of them. What should I do?
> Int *(*a[5])(int, char*);
> Void (*b[10]) (void (*)());
> 3. Doube (*) () (* pa) [9];
Answer and analysis:
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.

> Int *(*a[5])(int, char*);
//pFun is a type alias we created
Typedef int * (* pFun) (int, char *);
// use the new type defined to declare the object, which is equivalent to int* (*a[5])(int, char*);
PFun a, [5].
A is an array containing 5 elements. The element of the array is the function pointer. The return value of the function pointer refers to is the pointer to int

> Void (*b[10]) (void (*)());
// starts with the above expression The red part Declare a new type
PFunParam typedef void (*) ();
// declare a new type as a whole
Typedef void pFun (*) (pFunParam);
Void (*b[10]) (void (*)());
PFun b [10];
B is an array containing 10 elements, each element is a function pointer, the function pointer refers to the return value of the function is void, the input parameter is another function pointer, this function pointer has no input parameters, return value is void

> 3. Doube (*) () (* pa) [9];
// starts with the above expression The red part Declare a new type
Typedef double pFun (*) ();
// declare a new type as a whole
Typedef pFun (* pFunParam) [9];
// declare objects using the defined new type, equivalent to doube(*)() (*pa)[9];
PFunParam pa;

Pa is a pointer to an array of 9 elements. The element of the array is a function pointer


Related articles: