POINTERS TO FUNCTIONS

·

A function like a variable, has an address location in the memory. It is therefore, possible to declare a pointer to a function, which can then be used as an argument in another function. A pointer to a function is declared as follows:

type ( * fp) ( ) ;

This tells the compiler that fp is a pointer to a function which returns type value.

We can make a function pointer to point to a specific function by simply assigning the name of the function to the pointer.

For example,
double (*P1)( ), mul ( ) ;
P1 = mul ;
declare P1 as a pointer to a function and mul as a function and then make P1 to point to the
function mul. To call the function mul, we may now use the pointer P1 with the list of param- eters. That is,
(*P1) (x,y)
is equivalent to mul ( x,y )

About Me

Blog Archive