1. pointer to the function
Function is a pointer to return a pointer to the function , is the main function , the return value is a pointer
The basic form of the statement: return data type + + * function name + (variable type 1, ...) ;
int* fun(int,int); int * fun(int,int); int *fun(int,int);
These three statements are, more intuitive first return value is of type int *.
#include <stdio.h> int * Fun ( int * X) // pointer passed { int * X = tmp; // pointer tmp point X return tmp; // return address pointing tmp } int main () { int = b 2 ; int * p = & b; // address pointed to by p b of the printf ( " % D " , * Fun (p)); // the value of the output of the address pointed to by p return 0 ; }
Output: 2
2. The function pointer
Function pointer is a pointer to a function of the body is a pointer points to the address of a function
The basic form of the statement: return data type + (* function name) + (variable type 1, ...) ;
Note * and function names between braces, or because of priority reasons operators becomes a function pointer
int (*fun) (int);
#include <stdio.h> int the Add ( int X, int Y) { return X + Y; } int (Fun *) ( int , int ); // declare function pointer int main () { Fun = & the Add; // fun add function pointer to a function of the printf ( " % D " , fun ( . 3 , . 5 )); the printf ( " % D " , (* fun) ( . 4 , 2 )); return 0; }
输出结果:8 6
In the above examples, when the use of function pointers fun(3,5)
and (*fun)(3,5)
are
Parameters of the function parameter list function pointer to a pointer to a function and a list of the same
Remember that the main function is to return the pointer function pointer and function pointer is a pointer to a function
on that basis may have a function pointer and function pointer to a function pointer like
a pointer when the function declaration and function names do not need to * brackets
function pointer * requires when it was declared and the function name in parentheses (priority of reason)
3. Application of the pointer to the function
4. Application of function pointers