1. Function pointer
2. Function pointer for C++ class member function
3. Function returning function pointer
4. Function pointer passed as argument
5. Array of function pointers
Function pointer
Its a pointer holding the address of a function.
DeclarationExample function declaration - int myFunction(int, float);
Example function pointer - int (*myFuncPtr)(int, float);
Later in the program code assign the address of myFunction to myFuncPtr.
i.e., myFuncPtr = &myFunction;
Sample program
#include "stdafx.h"
//Declare a function
int myFuncAdd(int,int);
int myFuncMultiply(int,int);
//Declare function pointer1
int (*myAddPtr)(int,int);
int main(int argc, char* argv[])
{
myAddPtr = &myFuncAdd;
int result = myAddPtr(10,20);
printf("Added value %d\n",result);
//Using same function pointer for different function.
//You can do this as long as, function signatures are same.
myAddPtr = &myFuncMultiply;
result = myAddPtr(10,20);
printf("Multiplied value using add pointer %d\n",result);
//Declaration of function pointer2
int (*myMulPtr)(int,int) = &myFuncMultiply;
result = myMulPtr(10,20);
printf("Multiplied value %d\n",result);
return 0;
}
int myFuncAdd(int data1,int data2)
{
return data1 + data2;
}
int myFuncMultiply(int data1,int data2)
{
return data1 * data2;
}
-------------------------------------------------------------------------------------------------
Function pointer for C++ class member functionAll the non static C++ class member function pointer declaration contains a hidden parameter, which is the "this" pointer of instance of the class. So the function pointer for static member and non-static member are incompatible.
Example declaration
int (MyClass::*myFuncAddPtr)(int,int) = NULL;
Here myFuncAddPtr is a pointer to a function in a class called MyClass, which takes two integers as arguments and returns an integer.
Sample program
#include "stdafx.h"
class myClass
{
public:
int myFuncAdd(int data1,int data2)
{
return data1 + data2;
}
};
//Declare function pointer for myClass::myFuncAdd
int (myClass::*myFuncAddptr)(int,int) = NULL;
int main(int argc, char* argv[])
{
myClass myClassobj;
myFuncAddptr = &myClass::myFuncAdd;
result = (myClassobj.*myFuncAddptr)(20,30);
printf("Added value using C++ function pointers %d\n",result);
return 0;
}
-------------------------------------------------------------------------------------------------
Passing function pointer:Function pointer can be passed as a argument for a function. Below is the example for passing function pointer.
Returning function pointer:Function can return pointer to a function. This is bit tricky and confusing....see below for the declaration of the function call which return function pointer.
//Passing function pointer
int PassFuncPtr(int (*myFuncPtr)(int ,int ))
{
return myFuncPtr(10,60);
}
typedef int (*myFuncptr)(int,int);
myFuncptr retPointer()
{
int(*myFuncPtr)(int,int) = &myFuncAdd;
return myFuncPtr;
}
//Function definition with out using type def
int (*retPointer2())(int,int)
{
return &myFuncAdd;
}
inside main()
//Passing function pointer
int (*myFuncPtr)(int,int) = &myFuncAdd;
result = PassFuncPtr(myFuncPtr);
printf("Added value using passing function pointers %d\n",result);
//Returning function pointer
int (*myFuncPtr2)(int,int) = NULL;
myFuncPtr2 = retPointer();
result = myFuncPtr2(20,10);
printf("Added value using returned function pointer %d\n",result);
-------------------------------------------------------------------------------------------------
Array of function pointers: This is also bit tricky and confusing. I feel, using typedef is the easy way to implement array of function pointers.
below code is inside the main()
//Array of function pointers
typedef int (*myArrayFuncPtr)(int,int);
myArrayFuncPtr funcPtrArray[10] = {NULL};
//Without using typedef
int (*myArrayFuncPtr2[10])(int,int) = {NULL};
funcPtrArray[0] = &myFuncAdd;
result = (*funcPtrArray[0])(10,10);
printf("Added value using Array of function pointer %d\n",result);
-------------------------------------------------------------------------------------------------