I recently reviewed the C language pointer information and summarized the complex statements in C language.
The complex declaration here refers to interpreting the meaning of the declared code like the following.
int (*(*x)(int *,char *))(int);
(The code above declares a function pointer. This function receives an integer pointer and a character pointer as the parameter and function pointer. This function receives an integer parameter and returns an integer .)
The C language variable statement always implements two points:1. The declaration and the syntax used should be consistent as much as possible.
For example, declare a function pointer.
double (*fun)(double);
Use this function pointer
#include <math.h>fun=sin;doube reslut=(*fun)(0.5);//use point fun
2. The declaration statement is not based on the reading order from left to right, but on the priority of each symbol.
This is very important. It is used to determine whether a declaration declares a variable. There are three types of delimiters:
* () []
"*" Declares pointers and various pointers, including function pointers;
"()" Is specifically used to declare function pointers;
"[]" Is used to declare arrays.
The three operators "[]" and "()" have the highest priority. The combination law is "from right to left ".
After clarifying these two points, we can use these two points to interpret complex statements.
Specific rules: first, read the declared variable according to the priority and determine the variable to be declared. Next, one layer is split outward step by step. * Indicates the pointer; [] indicates the array; () indicates the function.
Example:
int (*(*x)(int *,char *))(int);
First, find X, and then find * X based on the priority. This proves that the entire statement declares a pointer. Then we can see that the right side of (* X) is a () character, which proves that X is a pointer to the function, since it is a function pointer, the rest is to describe the function return value type and parameter type. Then, you can see that the function input is an integer pointer and a character pointer. Then, take "(* X) (int *, char *)" as the whole, and find the * symbol with the highest priority to prove that the returned value is a pointer; then, if you find (INT), the pointer points to a function. The parameter of the function is an integer. Then, you can find the last Int, the returned value of this function is an integer. The explanation is complete.
Simply put, a function pointer is declared. This function receives an integer pointer and a character pointer as a parameter and returns a function pointer. The function pointing to this function receives an integer parameter, returns an integer.