quest4tech.net

 

Explanations of C and C++ Terms

Function Pointers and the Callback Model

Function Pointer

The function pointer, points to an address of a function having a known return type, a known number of parameters and each parameter is of a known type. The function pointed to is therefore of a defined type. Nothing else about the function, such as its name or source, needs to be known.

The function pointer can be used to point to different functions of the same defined type. In this way a program can be used to change between functions of the same type by changing the address pointed to by the function pointer. The program can be designed to make the change automatically at required times or when certain conditions are or are not fulfilled.

The function pointer can be used in any of the following ways:-

  1. pass in a function into another by defining the function address in the function parameters
     
  2. return a function from another by giving the function address as the return value.
     
  3. call a function from a field in a struct by placing its address there.
     
  4. call a function from a cell in an array by placing its address there.

Callback Model

A common application of function pointers is in a callback model. The function pointers are used to point to callback (or hook) functions. One or more callback functions are selected by the program and executed when certain events occur. In a server/client application the clients install the functions and cause them to be executed when certain events occur.

 

Return to Top

C/C++ index

quest4tech Index

(c) Compiled by B V Wood.

 Main Index
UK SPONSERS



USA SPONSERS