Learn C - C Function Pointer






Declaring a Pointer to a Function

The following code shows how to declare a pointer to a function.

int (*pfunction) (int);

The code above declares a variable that is a pointer to a function.

This statement just defines the pointer variable.

The name of the pointer is pfunction, and it points to functions with one parameter of type int and an int return type.

Calling a Function Through a Function Pointer

Suppose you define a function that has the following prototype:

int sum(int a, int b);

This function has two parameters of type int and returns type int. We can define a function pointer for it as follows.

int (*pfun)(int, int) = sum;

This declares a function pointer with the name pfun.

It can store addresses of functions with two parameters of type int and a return value of type int.

The statement also initializes pfun with the address of the function sum().

You can call sum() through the function pointer like this:

int result = pfun(4, 5);

This statement calls the sum() function through the pfun pointer with argument values of 4 and 5.

We can use the function pointer name just like a function name.

Suppose you define another function that has the following prototype:

int divide(int a, int b);

You can store the address of divide() in pfun with the following statement:

pfun = divide;

With pfun containing the address of divide(), you can call divide() through the pointer:

result = pfun(5, 12);

The following code has three functions with the same parameter and return types and uses a pointer to a function to call each of them.


    #include <stdio.h> 
//from  w ww . ja  v  a 2 s  . co m
    // Function prototypes 
    int sum(int, int); 
    int product(int, int); 
    int difference(int, int); 

    int main(void) { 
      int a = 10;                         
      int b = 5;                          
      int result = 0;                     
      int (*pfun)(int, int);              // Function pointer declaration 
      
      pfun = sum;                         // Points to function sum() 
      result = pfun(a, b);                // Call sum() through pointer 
      printf("pfun = sum             result = %2d\n", result); 
      
      pfun = product;                     // Points to function product() 
      result = pfun(a, b);                // Call product() through pointer 
      printf("pfun = product         result = %2d\n", result); 
      pfun = difference;                  // Points to function difference() 
      result = pfun(a, b);                // Call difference() through pointer 
      printf("pfun = difference      result = %2d\n", result); 
      return 0; 
    } 

    int sum(int x, int y) { 
      return x + y; 
    } 
      
    int product(int x, int y) 
    { 
      return x * y; 
    } 
      
    int difference(int x, int y) 
    { 
      return x - y; 
    } 

The code above generates the following result.





Arrays of Pointers to Functions

You can create an array of pointers to functions.

To declare an array of function pointers, put the array dimension after the function pointer array name.

int (*pfunctions[10]) (int);

This declares an array, pfunctions, with ten elements.

Each element in this array can store the address of a function with a return type of int and a parameter of type int.

The following code shows how to use an array of pointers to functions.


     #include <stdio.h> 
      //from w  w w  . j  av a 2 s .com
     // Function prototypes 
     int sum(int, int); 
     int product(int, int); 
     int difference(int, int); 
      
     int main(void) { 
          int a = 10;   
          int b = 5;    
          int result = 0;
          int (*pfun[3])(int, int);           // Function pointer array declaration 
    
          // Initialize pointers 
          pfun[0] = sum; 
          pfun[1] = product; 
          pfun[2] = difference; 
          
          // Execute each function pointed to 
          for(int i = 0 ; i < 3 ; ++i) { 
            result = pfun[i](a, b);           // Call the function through a pointer 
            printf("result = %2d\n", result); 
          } 
          
          // Call all three functions through pointers in an expression 
          result = pfun[1](pfun[0](a, b), pfun[2](a, b)); 
          printf("The product of the sum and the difference = %2d\n", result); 
          return 0; 
     } 
    int sum(int x, int y) { 
      return x + y; 
    } 
      
    int product(int x, int y) 
    { 
      return x * y; 
    } 
      
    int difference(int x, int y) 
    { 
      return x - y; 
    } 

The code above generates the following result.





Pointers to Functions As Arguments

You can use a pointer to a function as an argument to a function.

In this way we can call different function depending on which function is pointed by the pointer.


    #include <stdio.h> 
      /*from  w ww  . j  av a  2  s  .c om*/
    // Function prototypes 
    int sum(int,int); 
    int product(int,int); 
    int difference(int,int); 
    int any_function(int(*pfun)(int, int), int x, int y); 
      
    int main(void) { 
      int a = 10;  
      int b = 5;   
      int result = 0;
      int (*pf)(int, int) = sum;          // Pointer to function 
      
      // Passing a pointer to a function 
      result = any_function(pf, a, b); 
      
      printf("result = %2d\n", result ); 
      
      // Passing the address of a function 
      result = any_function(product,a, b); 
      
      printf("result = %2d\n", result ); 
      
      printf("result = %2d\n", any_function(difference, a, b)); 
      return 0; 
    } 
    // Definition of a function to call a function 
    int any_function(int(*pfun)(int, int), int x, int y) { 
      return pfun(x, y); 
    } 
    int sum(int x, int y) { 
      return x + y; 
    } 
    int product(int x, int y) { 
      return x * y; 
    } 
    int difference(int x, int y) { 
      return x - y; 
    } 

The code above generates the following result.