The main reference books in this article are C ++ Primer.
Note:
The function pointed to by the function pointer must be a global function or a static function of the class.
A function pointer is a pointer to the start address of a function. It can be considered as a variable of the function name,
Example: void f (int I) {printf ("% d", I-1 );}
// Function. f is the constant void g (int I) {printf ("% d", I + 1 );}
// Function, g is the constant void out (void (* foo) (int) {// function pointer foo is the variable foo (5 );
// Which function is called depends on whether the foo parameter is assigned a value of f or g}
void main() { out(f); out(g);}
Output: 46
Void (* foo) (int) is interpreted as follows: foo is a pointer pointing to a function. The function has an int parameter and has no return value.
Int (* fp) (int a); // a pointer to the function is defined here.
Function pointers cannot point to different types or functions with different parameters. When defining function pointers, we can easily make the following mistakes.
Int * fp (int a); // here it is incorrect, because according to the combination and priority, it is first combined with (), and then becomes a function that returns an integer pointer, instead of function pointers, pay special attention to this!
The simplest function pointer is defined as follows:
Void (* funcPtr) (); // funcptr is a function pointer. It points to a function without parameters and returns void.
Very complex function pointer definitions:
1. Void *(*(* Fp1) (int)) [10];
Resolution: Void * X [10]; this is an array with 10 void * elements.
X = *(* Fp1) (int)(* Fp1) (int) is a pointer pointing to an array. This array has 10 void pointer elements.
Fp1 is a function pointer and its parameter is an int type parameter. The return value is a pointer to an array containing 10 void * elements.
2. Void * (* fp1) (int) [10];
Resolution: void * X [10]; è X is an array with 10 void * types.
X = (* fp1) (int );
Fp1 is a function pointer and has an integer parameter. Its return value is an array of 10 void * elements.
If this form is Void * (* fp1) (int) [10], the return value is a pointer to an array containing 10 voids.
3. Void (* fp1) (int) [10];
Resolution: void X [10] è X is an array of 10 void types.
X = (* fp1) (int );
Fp1 is a function pointer and has an integer parameter. Its return value is an array with 10 void elements.
In this form, Void (* fp1) (int) [10] returns an array of 10 void types.
4. Float(* Fp2) (int, int, float ))(Int );
Resolution: Float X (int) è X is a function with an integer parameter. The returned value is a floating point number.
X =* (* Fp2) (int, int, float );
Fp2 is a function pointer. It has three parameters: the first two are Integer type, and the last one is floating point type. The return value is a pointer to a function (function pointer ). The prototype of the function pointer pointing to is only one integer parameter, and the return value is a floating point number.
5. Typedef double (* fp3) () [10]) ();
Resolution: fp3 is a function pointer without parameters. The returned value is a pointer.
X = (* fp3 )())
Typedef double (* X [10]) ()
X is an array with 10 elements. all its elements are function pointers. The function prototype pointed to by this function pointer is a function with no parameters and the return value is double type.
In summary, fp3 is a newly defined data type, which is a function pointer. The function prototype that this function pointer points to is a function without parameters, and the return value is a pointer to an array. This array has 10 elements and its element type is a function pointer. The function prototype pointed to by this function pointer is a function with no parameters and the return value is a double type function.
6. Int (* fp4 () [10]) ();
Resolution: fp4 is a function with no parameters. The returned value is a pointer;
X = (* fp4 ());
Int (* X [10]) ();
X is an array with 10 elements. Each element is a function pointer. This function pointer represents a function prototype with no parameters and the return value is an integer function.
In combination, fp4 is a function with no parameters. When the returned value is a pointer to the data, this array has 10 elements, each element is a function pointer, the function circle represented by this function pointer is a function with no parameters and the return value is an integer.