- Function Pointer are similar to Pointers , Only difference is instead of pointing to a variables , function pointer points to Functions.
Let us go through one example for better understanding:
- int Arr[5]
As we know Arr is constant pointer to an 5 element array , in order to retrieve value we will deference the pointer either by Arr[index] or *Arr, the corresponding Array values are returned.
- int fun();
If you guessed that fun is actually a constant pointer to a function, you are correct. When a function is called (via the () operator), the function pointer is deferenced, and execution branches to the function.
Similar way is possible to declare a non-constant pointer to a function, syntax for writing a non -constant pointer to a function is :
- int (*fun)(int);
Parenthesis are necessary for precedence reason else it will be interpreted as pointer to a integer instead of pointer to a function.
fun is a pointer to a function that takes one integer parameter and return an integer , it will point to any function that matches its Signature.
example:
#include<iostream>
#include<conio.h>
using namespace std;
int fun(int a)
{
a++;
return a;
}
int main()
{
int (*Pfun)(int);
Pfun = fun;
int b = Pfun(10);
cout<<"\n value returned from pointer to a function is:-"<<a<<"\n";
return 0;
}
example:
#include<iostream>
#include<conio.h>
using namespace std;
int fun(int a)
{
a++;
return a;
}
int main()
{
int (*Pfun)(int);
Pfun = fun;
int b = Pfun(10);
cout<<"\n value returned from pointer to a function is:-"<<a<<"\n";
return 0;
}
Note: the signature (parameters and return value) of the function pointer must match the signature of the function. lets see few examples for better understanding.
// function prototypes
int foo();
double goo();
int hoo(int nX);
// function pointer assignments
int (*pFcn1)() = foo; // okay
int (*pFcn2)() = goo; // wrong -- return types don't match!
double (*pFcn3)() = goo; // okay
pFcn1 = hoo; // wrong -- pFcn1 has no parameters, but hoo() does
int (*pFcn3)(int) = hoo; // okay
few Examples of Pointer to function:
#include<iostream>
#include<conio.h>
#include<stdio.h>
using namespace std;
//Example 1: simple
If a function pointer needs to point to functions that take parameters, the data-types of these parameters can simply be separated by commas in the function pointer declaration. Take for example the following:
int fun(int a , int b)
{
return a+b;
}
int main()
{
int (*Pfun)(int, int );
Pfun = fun;
int a= Pfun(10,12);
cout<<"\n value returned from pointer to a function is:-"<<a<<"\n";
return 0;
}
//Example 2:
if you want that caller function have flexibility to control how operation need to be done by simply reusing old snippet of code in-spite of writing two separated functions.
like in below code snippet function fun is being used both for add and multiply in-spite of writing separate fun function for add and multiply.
int fun(int a,int (*operation)(int , int))
{
return a+operation(10,5);
}
int add(int a, int b)
{
return a+b;
}
int multiply(int a, int b)
{
return a*b;
}
int main()
{
int x = fun(10,add);
int y = fun(12,multiply);
//int b = Pfun(10);
cout<<"\n value returned from pointer to a function is:-"<<x<<"\n";
cout<<"\n value returned from pointer to a function is:-"<<y<<"\n";
return 0;
}
//Example 3:
We can also create arrays of function pointers. These aren't as scary as they sound, and they're simply created much like you would with an array of "normal" pointers, by specifying a number of elements in square brackets next to the pointer name. If we wanted a function pointer array which contained pointers to both functions "add" and "multiply" in elements of index 0 and 1, we could use the following:
int add(int a, int b)
{
return a+b;
}
int multiply(int a, int b)
{
return a*b;
}
int main()
{
int (*fun[2])(int , int);
fun[0]= add;
fun[1]= multiply;
int x = fun[0](10,12);
int y =fun[1](1,3);
cout<<"\n value returned from pointer to a function is:-"<<x<<"\n";
cout<<"\n value returned from pointer to a function is:-"<<y<<"\n";
return 0;
}
// function prototypes
int foo();
double goo();
int hoo(int nX);
// function pointer assignments
int (*pFcn1)() = foo; // okay
int (*pFcn2)() = goo; // wrong -- return types don't match!
double (*pFcn3)() = goo; // okay
pFcn1 = hoo; // wrong -- pFcn1 has no parameters, but hoo() does
int (*pFcn3)(int) = hoo; // okay
few Examples of Pointer to function:
#include<iostream>
#include<conio.h>
#include<stdio.h>
using namespace std;
//Example 1: simple
If a function pointer needs to point to functions that take parameters, the data-types of these parameters can simply be separated by commas in the function pointer declaration. Take for example the following:
int fun(int a , int b)
{
return a+b;
}
int main()
{
int (*Pfun)(int, int );
Pfun = fun;
int a= Pfun(10,12);
cout<<"\n value returned from pointer to a function is:-"<<a<<"\n";
return 0;
}
//Example 2:
if you want that caller function have flexibility to control how operation need to be done by simply reusing old snippet of code in-spite of writing two separated functions.
like in below code snippet function fun is being used both for add and multiply in-spite of writing separate fun function for add and multiply.
int fun(int a,int (*operation)(int , int))
{
return a+operation(10,5);
}
int add(int a, int b)
{
return a+b;
}
int multiply(int a, int b)
{
return a*b;
}
int main()
{
int x = fun(10,add);
int y = fun(12,multiply);
//int b = Pfun(10);
cout<<"\n value returned from pointer to a function is:-"<<x<<"\n";
cout<<"\n value returned from pointer to a function is:-"<<y<<"\n";
return 0;
}
//Example 3:
We can also create arrays of function pointers. These aren't as scary as they sound, and they're simply created much like you would with an array of "normal" pointers, by specifying a number of elements in square brackets next to the pointer name. If we wanted a function pointer array which contained pointers to both functions "add" and "multiply" in elements of index 0 and 1, we could use the following:
int add(int a, int b)
{
return a+b;
}
int multiply(int a, int b)
{
return a*b;
}
int main()
{
int (*fun[2])(int , int);
fun[0]= add;
fun[1]= multiply;
int x = fun[0](10,12);
int y =fun[1](1,3);
cout<<"\n value returned from pointer to a function is:-"<<x<<"\n";
cout<<"\n value returned from pointer to a function is:-"<<y<<"\n";
return 0;
}
0 comments:
Post a Comment