In the C language, it is easy for people to get dizzy when they encounter pointers. Especially, although I know the pointer myth and the C language, I also know the trouble of pointers.
Today, when I read a book and saw a pointer to a function, I suddenly fell into a self-chaos state. This is an escape of psychological chaos! But I always need to understand it slowly, so I will try my best to summarize it based on what I see on the Internet and what I have read in the book, so that I can refer to it later...
In C, the function itself is not a variable, but can define a pointer to the function. Pointers of this type can be assigned values, stored in arrays, passed to functions, and returned values as functions. This basically shows why a function pointer is required. In my understanding, a function itself cannot be used as a function parameter, So we define a pointer to the function so that it can be used as a parameter in the function. This may be the most important use of function pointers!
Baidu encyclopedia understands that a function pointer is a pointer variable pointing to a function. Therefore, the "function pointer" should be a pointer variable first, except that the pointer variable points to the function. This is just like using pointer variables to point to integer variables, struct variables, and arrays. Here we point to functions. As mentioned above, during C compilation, each function has an entry address, which is the address pointed to by the function pointer. With the pointer variable pointing to the function, the function can be called using the pointer variable, just like using the pointer variable to reference other types of variables. These concepts are consistent. Function pointers have two purposes: Calling functions and making function parameters.
Note 1: "function type" indicates the return type of the function. Because "()" has a higher priority than "*", parentheses outside the pointer variable name are required, the following "parameter list" indicates the parameter list of the function to which the pointer variable points. For example: