In C A function pointer is a type of pointer. When dereferenced, a function pointer can be used to invoke a function and pass it arguments just like a normal function. Function pointers can be used to simplify code by providing a simple way to select a function to execute based on run-time values.

Function pointers do always point to a function having a specific signature. Thus, all functions used with the same function pointer must have the same parameters and return type.

Function pointers are often used to replace switch statements. In the following program I'll show you how to do this job.

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

void runOper2(float a, float (*func)(float));
void runOper(float a, char oper);
float Sqr(float a);
float Sqrt(float a);
float Log(float a);
int main(int argc, char **argv);

float Sqr(float a)
{
    return a*a;
}

float Sqrt(float a)
{
    return sqrt(a);
}

float Log(float a)
{
    return log(a);
}

void runOper(float a, char oper)
{
   float result;

   switch(oper)
   {
      case 's' :
            result = Sqr(a)
            break;
      case 'q' :

            result = Sqrt(a)
            break;
      case 'l' :
            result = Log(a)
            break;
   }

   printf("result = %f\n", result);
}

void runOper2(float a, float (*func)(float))
{
   float result = func(a);
   printf("result = %f\n", result);
}

// Main program
int main(int argc, char **argv)
{

    runOper(2, 's');
    runOper2(2, &Sqr);

    runOper(2, 'q');
    runOper2(2, &Sqrt);

    runOper(2, 'l');
    runOper2(2, &Log);
    return(EXIT_SUCCESS);

}

 

The two calls runOper and runOper2 are equivalent but using runOper2 you shall write less code.

 

Callback Functions

Another use for function pointers is setting up "listener" or "callback" functions that are invoked when a particular event happens.

One example is when you're writing code for a GUI. Most of the time, the user will interact with a loop that allows the mouse pointer to move and that redraws the interface. Sometimes, however, the user will click on a button or enter text into a field. These operations are "events" that may require a response that your program needs to handle. How can your code know what's happening? Using Callback functions! The user's click should cause the interface to call a function that you wrote to handle the event. 

Consider what might happen if you were using a GUI library that had a "create_button" function. It might take the location where a button should appear on the screen, the text of the button, and a function to call when the button is clicked. Assuming for the moment that C (and C++) had a generic "function pointer" type called function, this might look like this:

void create_button( int x, int y, const char *text, function callback_func );

Whenever the button is clicked, callback_func will be invoked. Exactly what callback_func does depends on the button; this is why allowing the create_button function to take a function pointer is useful.

Gg1