A function pointer is a variable that stores the address of a function that can later be called through that function pointer. This is useful because functions encapsulate behavior.
Declaring a Pointer to a Function
Declaration of pointers to functions resembles other declarations. For example, just as we write
int *p;
to say that *p has type int and implying that p is a pointer, we may write
int (*pf)(int);
to say that if we dereference pf, and call it with an int argument, the result has type int. By implication, pf is a pointer to a function that takes an int argument and returns an int result.
A pointer to a function is similar to a pointer to data but it should always be enclosed in parenthesis when using the dereference operator (*) to avoid an compilation error. It then be followed by another parenthesis containing any arguments to be passed to the function when using the *.
Because all that we can do with a function is to take its address or call it, any use of a function that is not a call is assumed to be taking its address, even without an explicit &.
Suppose we have a function with the following prototype:
int f(int); //prototype
Note that it look just like our declaration of pointer to a function:
int (*pf)(int);
Because f is a function, so is (*pf). And if (*pf) is a function, then pf must be a pointer to a function.
The declaration requires the parentheses around *pf to provide the proper operator precedence. Parentheses have a higher precedence than the * operator, so *pf(int) means pf() is a function that returns a pointer while (*pf)(int) means pf is a pointer to a function.
int (*pf)(int); // pf points to a function that returns int
int *pf(int); // pf() is a function that returns a pointer to int
Once we declare pf properly, we can assign to it the address of a matching function:
int f(int);
int (*pf)(int);
pf = f; // pf now points to the f() function
Invoking a Function Using a Pointer
As we discussed in the previous section, (*pf) serves as a name of a function. In other words, function pointers are invoked by name just like normal function calls.
int f(int);
int (*pf)(int);
pf = f; // pf now points to the f() function
int i = f(4); // call f() using the function name
int j = (*pf)(4); // call f() using the pointer pf
int k = pf(5) // also call f() using the pointer pf
The code below shows very simple usage of function pointer:
#include <iostream>
using namespace std;
int square(int n) {
return n*n;
}
int main()
{
int (*pf)(int);
pf = square;
cout << "square(7) = " << pf(7) << endl;
return 0;
}
Here is another example using a pointer to a function.
#include <iostream>
using namespace std;
int operation (int i, int j, int (*pf)(int,int)) {
int k;
k = (*pf)(i,j);
/* This works, too.
k = pf(i,j)
*/
return k;
}
int addition (int a, int b) {
return a+b;
}
int division (int a, int b) {
return a/b;
}
int main()
{
int m = operation (10, 5, addition);
int n = operation (10, 5, division);
cout << "m = " << m << endl;
cout << "n = " << n << endl;
return 0;
}
Output is:
m = 15
n = 2
For More Please refer the link
http://www.bogotobogo.com/cplusplus/pointers3_function_multidimensional_arrays.php