JustKernel

Ray Of Hope

Function Pointer/ Pointers to Function

Pointers to Function / Function Pointers.

A useful technique is the ability to have pointers to functions (Specially useful in case of implemening State Machine ).

Suppose you declare

int func (int , int);

The pointer to a function will look like

int (*funcptr)(int a, int b ) //pointer to a funtion returning int.

Note:

int * func(int a , int b);// function returning pointer to int.

Because of precedence if you don’t parenthize the name , you declare a function returning a pointer.

Once you have got the pointer , you can assign the address of the right sort of function just by using its name : like an array, a function name is turned to an address when its used in an expression. You can call the function as

funcptr = func;

(*funcptr) (a, b);

A sample program

#include <stdio.h>

void func(int)

main

{

void (*fp)(int);

fp = func;

(*fp)(1);

}

void func (int arg)

{

printf(“%d”, args);

}

Similarly, you can declare array of function pointers

void (*funcptr[5])(int);

funcptr[0] = func1;

funcptr[1] = func2;

Call the functions as

(*funcptr[0])(10);

(*funcptr[1])(20);

How to Return a Function Pointer ?
It’s a little bit tricky but a function pointer can be a function’s return value. In the following example there
are two solutions of how to return a pointer to a function which is taking a float and returns two float. If you
want to return a pointer to a member function you have just got to change the definitions/declarations of all
function pointers.
//—————————————————————————————–
// 2.7 How to Return a Function Pointer
// ’Plus’ and ’Minus’ are defined above in 2.1-4. They return a float and take two float
// direct solution
// function takes a char and returns a pointer to a function which is taking two
// floats and returns a float. <opCode> specifies which function to return
float (*GetPtr1(const char opCode))(float, float)
{
if(opCode == ’+’) return &Plus;
if(opCode == ’-’) return &Minus;
}
// solution using a typedef
// define a pointer to a function which is taking two floats and returns a float
typedef float(*pt2Func)(float, float);
5
// function takes a char and returns a function pointer which is defined as a
// type above. <opCode> specifies which function to return
pt2Func GetPtr2(const char opCode)
{
if(opCode == ’+’) return &Plus;
if(opCode == ’-’) return &Minus;
}
// execute example code
void Return_A_Function_Pointer()
{
cout << endl << “Executing ’Return_A_Function_Pointer’” << endl;
float (*pt2Function)(float, float); // define a function pointer
pt2Function=GetPtr1(’+’); // get function pointer from function ’GetPtr1’
cout << pt2Function(2, 4) << endl; // call function using the pointer
pt2Function=GetPtr2(’-’); // get function pointer from function ’GetPtr2’
cout << pt2Function(2, 4) << endl; // call function using the pointer
}

One More sample

#include<stdio.h>

void fn(int a)
{
printf(“\n %d”, a);
}

typedef void (*fnptr)(int);
fnptr fn1()
{
return fn;
}

void main()
{
fnptr tmp;
tmp = fn1();
tmp(10);
}

Anshul Makkar, anshul_makkar@justkernel.com

Originally Posted on: 2010-04-30 04:14:54

Tags:


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.