1 2 3 4 5 6 7 8 9

Pointers to Functions

Use of a pointer to a function provides an elegant solution to some problems.

A pointer to a function holds the memory address of the function's executable code
Using (dereferencing) the pointer is the same as calling the function.

Function pointers can only be used in the following way:

* assignment to(specify function name)
*
assignment from(make a copy)
*
dereference(that is, make function call)

Consider the prototype for some function (here display):

int display ( int, char* );

we can declare the existence of a function pointer fptr:

int (*fptr)();

The general form of the syntax is:

type (*pointer)();

Initialize the pointer by simply assigning the name to it.

fptr = display;

4 February, 1998

2

Copyright University of Alberta