Pointers To Functions In C Language

Pointers to Functions:

A function even though not a variable still has a physical address in memory and this address may be assigned to a pointer. When a function is called it essentially causes an execution jump in the program to the location in memory where the instructions contained in the function are stored so it is possible to call a function using a pointer to a function.

The address of a function is obtained by just using the function name without any parentheses, parameters or return type in much the same way as the name of an array is the address of the array.

A pointer to a function is declared as follows

Syntax
ret_type  ( * fptr ) ( parameter list );

where fptr is declared to be a pointer to a function which takes parameters of the form indicated in the parameter list and returns a value of type ret_type.

The parentheses around * fptr are required because without them the declaration

Syntax
ret_type  * fptr( parameter list ) ;

just declares a function fptr which returns a pointer to type ret_type !

To assign a function to a pointer we might simply do the following

Syntax
int (*fptr)( ) ;
fptr = getchar ;    /* standard library function */

To call the function using a pointer we can do either of the following

Syntax
ch = (*fptr)( ) ;
ch = fptr( ) ;

Example :- Program to compare two strings using a comparison function passed as a parameter.

CODE/PROGRAM/EXAMPLE
#include <stdio.h>
#include <string.h>
void check( char *a, char *b, int ( * cmp ) ( ) );

void main( )
{
char s1[80], s2[80] ;
int (*p)( ) ;

p = strcmp ;

gets(s1) ;
gets( s2 );

check( s1, s2, p ) ;
}
void check ( char *a, char *b, int (* cmp)( ) )
{
if ( ! cmp( a, b ) )
	puts( "equal" ) ;
else
	puts( "not equal") ;
}

Note that even though we do not specify parameters to the function pointer in the prototype or declarator of the function we must specify them when actually calling the function.

Note also that instead of using an explicitly declared pointer variable to call the required function in main() we could make the call as follows

Syntax
check( s1, s2, strcmp ) ;

where we essentially pass a constant pointer to strcmp( ).

For Example : Program that may check for either numeric or alphabetic equality.

CODE/PROGRAM/EXAMPLE
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>

void check( char *a, char *b, int ( * cmp ) ( ) );
int numcmp( char *, char * ) ;

void main( )
{
char s1[80], s2[80] ;

gets(s1) ;
gets( s2 );

if ( isalpha( *s1 )             // should have a more rigorous test here
	check( s1, s2, strcmp ) ;
else
	check( s1, s2, numcmp ) ;
}
void check ( char *a, char *b, int (* cmp)( ) )
{
if ( ! cmp( a, b ) )
	puts( "equal" ) ;
else
	puts( "not equal") ;
}

int numcmp( char *a, char *b )
{
if ( atoi( a ) == atoi( b ) )
	return 0 ;
else
	return 1 ;
}
#pointers_to_functions_in_c_language #Pointers_to_Functions_in_c #isalpha_in_c #cmp_in_c #strcmp__in_c

(New page will open, for Comment)

Not yet commented...