Pointers And Functions

Every type of variable with the exception of register, has an address. we have seen how we can reference variable of type char, int, float etc. through their addresses - that is by using pointers. Pointers can also point to C functions. And why not? C functions have addresses. If we know the function's address we can point to it, which provides another way to evoke it. Let us see how this can be done.

main( )

{

int display( ) ;

printf ( "\nAddress of function display is %u",display ) ;

display( ) ; /* usual way of invoking a function*/

}

display( )

{

printf ( "\n Long live viruses!!" ) ;

}

The output of the program would be:
Address of function display is 1125

Long live viruses!!

Note that to obtain the address of a function all that we have to do is to mention the name of the function, as has been done in printf( ) statement above. This is similar to mentioning the name of the array to get its base address.

Now let us see how using the address of a function we can manage to invoke it. This is shown in the program given below:

/* Invoking function using pointer to a function */

main( )

{

int display( ) ;

int ( *func_ptr )( ) ;

func_ptr = display ; /* assign address of function */

printf ( "\nAddress of function display is %u", func_ptr ) ;

( *func_ptr )( ) ;

/* invokes the function display( ) */

}

display( )

{

printf ( "\nLong live viruses!!" ) ;

}

The output of the program would be:
Address of function display is 1125

Long live viruses!!

In main( ) we declare the function display( ) as a function returning an int. But what are we to make of the declaration,

int ( *func_ptr )( ) ;

that comes in the next line? We are obviously declaring something which, like display( ), will return an int. But what is it? And why is *func_ptr enclosed in parentheses?

If we glance down a few lines in our program, we see the statement,

func_ptr = display ;

So we know that func_ptr is being assigned the address of display( ) . Therefore, func_ptr must be a

pointer to the function display( ).

Thus, all that the declaration

int ( *func_ptr )( ) ;

means is, that func_ptr is a pointer to a function, which returns an int. And to invoke the function we are just required to write the statement,

( *func_ptr )( ) ;

As we have seen, we can have an array of pointers to a int, float, string and structure similarly we can have an array of pointers to a function. It is illustrated in following program.

main( )

{

int ( *p [ 3 ] ) ( int, float ) ;

int i ;

void fun1 ( int , float ) ;

void fun2 ( int , float ) ;

void fun3 ( int , float ) ;

clrscr( ) ;

p [ 0 ] = fun1 ;

p [ 1 ] = fun2 ;

p [ 2 ] = fun3 ;

for ( i = 0; i <= 2; i++ )

( *p [ i ] ) ( 10, 3.14 ) ;

getch( ) ;

}

void fun1 ( int a, float b )

{

printf ( "\na = %d b = %f",a, b ) ;

}

void fun2 ( int c, float d )

{

printf ( "\nc = %d d = %f",c, d ) ;

}

void fun3 ( int e, float f )

{

printf ( "\ne = %d f = %f",e, f ) ;

}

In the above program we take an array of pointers to function int ( *p[3] ) ( int, float ) We store the addresses of three function f1( ), f2( ), f3( ) in array ( int *p[ ] ). In for loop we consecutively call each function using their addresses stored in array.

The output of the program would be:

i = 10 j = 3.14
a = 10 b = 3.14
x = 10 y = 3.14

0 comments: