Cute FTP Pro

More Pointers - IV


The following topics are covered in this section:


Pointers to Functions

You can even create pointers that point to functions. The primary use of such pointers is to pass functions as arguments to another function. You have to be very careful while declaring pointers to functions. Take note of the position of the parentheses:

return-data (*pointer-name) (arguments);

We’ll write a simple program to add two numbers. For this purpose we shall define a function called sum( ). This function sum ( ), will be passed to another function called func( ) which will simply call the sum ( ) function.

#include <iostream.h>
int sum(int a, int b)
{
return (a+b);
}

void func(int d, int e, int (*p1)(int,int))
{
cout<<"The result is : "<<(*p1)(d,e);
}

int main( )
{
int (*p)(int,int);
p=sum;
func(5,6,p);
return 0;
}

The output is:

The result is : 11

Though the program is simple a few statements might appear confusing. The statement

int (*p) (int,int);

declares a pointer to a function that has a return value of integer and takes two arguments of type integer.

sum( ) is a function with return data type of integer and also with two arguments of type integer. Hence the pointer ‘p’ can point to the function sum ( ). Thus we assign the address of the function sum ( ) to ‘p’:

p = sum;

We’ve also defined another function called as ‘func ( )’ which takes two integer arguments and a third argument which is a pointer to a function. The idea is to pass the function sum ( ) to the function ‘func( )’ and call the sum ( ) function from func ( ).

func(5,6,p);

will call the func ( ) function and it will pass pointer ‘p’ to func ( ). Remember that p is a pointer to sum ( ). Hence in reality we are actually passing a function as argument to another function.

Let’s expand the program one step further by creating another function called product( ) which will return the product of the two arguments.

#include <iostream.h>
int sum(int a, int b)
{
        return (a+b);
}

int product(int x,int y)
{
        return (x*y);
}

void func(int d, int e, int (*p1)(int,int))
{
        cout<<endl<<"The result is : "<<(*p1)(d,e);
}

int main( )
{
        int (*p)(int,int);
        p=sum;
        func(5,6,p);
        p=product;
        func(5,6,p);
        return 0;
}

The output is:

The result is : 11

The result is : 30

The program is very similar to the previous one. I just wanted to illustrate that you can pass any function to func ( ) as long as it has a return type of integer and it has two integer arguments.

Practical use: You might be wondering why we’d need to pass functions as arguments to other functions. There are instances when you might create generic functions, which the user can tailor to suit his/her needs. For example: Signal handling functions usually contain a function as one of their arguments. These functions are used in a program to react to certain external events- for example if the user presses CONTROL+C keys, the default action is for the program to terminate. But you might write a program in which you want to print “YOU CAN’T TERMINATE THIS PROGRAM” every time the user presses CONTROL+C. To perform this there are standard signal handling functions. These functions generally take the following 2 arguments:

1.)    The signal for which you want your program to react (CONTROL+C is just one of the possible signals your program might receive).

2.)    The function which has to be performed when your program receives that signal.

The function might be something like:

            int sighandler (int sig, pointer-to-function);

All the user needs to do is decide what signal he wants to handle and then decide on the function he wants to perform in case that signal is received (there’s no need to rewrite the entire signal handling function for his application; which would be time consuming. Programming is all about reusing code rather than rewriting). Can you think of any other simpler way in which a generic function like sighandler can be created?


Pointer Declarations

Pointer declarations are a bit confusing. Read through the various declarations described below to clear your doubts:

Declaration What it means
int *p;  pointer to an integer
void p(char *str); function that accepts a pointer to a character
int (*p)(char *str);  pointer to a function that accepts pointer to character as argument and returns an integer.
int *p(char *str); p is a function that accepts pointer to character as argument and returns a pointer to integer.
int *p[5];  p is an array of pointers to integers
int (*p)[5]; p is pointer to an integer array of 5 elements
void (*p)(char (*str)[] ); pointer to function that accepts pointer to character array

The list can be made more complicated but this should be sufficient to understand how pointers are generally declared. A couple of declarations are pretty interesting: 

int *p[5];                                  -           p is an array of pointers to integers 

int (*p)[5];                                -           p is pointer to an integer array of 5 elements 

The subscript operator has a higher precedence over * in the above declaration. So if we say:

            int *p[5];

the compiler will create an array of pointers. To forcefully evaluate *p first, we use the parentheses and declare:

            int (*p)[5];

Now the compiler considers ‘p’ as a pointer and so this means that ‘p’ is a pointer to an array of integers.


Go back to the Contents Page


Copyright © 2004 Sethu Subramanian All rights reserved.