Skip to main content
Engineering LibreTexts

12.7: Function Pointer in C

  • Page ID
    29110
  • Function Pointer in C

    In C, like normal data pointers (int *, char *, etc), we can have pointers to functions. Following is a simple example that shows declaration and function call using function pointer.

    #include <iostream>
    using namespace std;
    // A normal function with an int parameter
    // and void return type
    void fun(int a)
    {
        cout << "Value of a is " << a << endl;
    }
    
    int main()
    {
        // Declare a void function pointer
        // with a single int argument
        void (*fun_ptr)(int);
        // Assign the function pointer the address of the function
        fun_ptr = &fun;
        
        // Invoking fun() using fun_ptr
        (*fun_ptr)(10);
    
        return 0;
    } 
    

    This is the same as what we have already learned about pointers...just that we are now pointing at functions. The output would be what we expect:

    Value of a is 10

    Why do we need an extra bracket around function pointers like fun_ptr in above example?
    If we remove bracket, then the expression “void (*fun_ptr)(int)” becomes “void *fun_ptr(int)” which is declaration of a function that returns void pointer. 

    Following are some interesting facts about function pointers.

    1) Unlike normal pointers, a function pointer points to code, not data. Typically a function pointer stores the start of executable code.

    2) Unlike normal pointers, we do not allocate de-allocate memory using function pointers.

    3) A function’s name can also be used to get functions’ address. For example, in the below program, we have removed address operator ‘&’ in assignment. We have also changed function call by removing *, the program still works.

    #include <iostream>
    using namespace std;
    // A normal function with an int parameter
    // and void return type
    void fun(int a)
    {
        cout << "Value of a is " << a << endl;
    }
    
    int main()
    {
        // Declare a void function pointer
        // with a single int argument
        void (*fun_ptr)(int);
        // Assign the function pointer the address of the function
        fun_ptr = fun;
        
        // Invoking fun() using fun_ptr
        fun_ptr(10);
    
        return 0;
    } 

    Program works, output is the same:

    Value of a is 10

     
    4) Like normal pointers, we can have an array of function pointers. Below example in point 5 shows syntax for array of pointers.

     
    5) Function pointer can be used in place of switch case. For example, in below program, user is asked for a choice between 0 and 2 to do different tasks.

    #include <iostream>
    using namespace std;
    void add(int a, int b)
    {
        cout << "Addition is "  << a+b << endl;
    }
    void subtract(int a, int b)
    {
        cout << "Subtraction is "  << a-b << endl;
    }
    void multiply(int a, int b)
    {
        cout << "Multiplication is "  << a*b << endl;
    }
    
    int main()
    {
        // fun_ptr_arr is an array of function pointers
        void (*fun_ptr_arr[])(int, int) = {add, subtract, multiply};
        unsigned int ch, a = 15, b = 10;
    
        for (int count = 0; count < 3; count++)
        {
            ch = unsigned (count);
           (*fun_ptr_arr[ch])(a, b);
        }
        return 0;
    } 

    This is a bit more complex, the key is that the function pointer is an array - pointing at the 3 functions. In the for loop the value of ch is 0, 1 and 2 - thereby calling the addition function, fun_ptr_arr[0], the subtraction function, fun_ptr_arr[1], and the multiplication function, fun_ptr_arr[2].

    Also, the assignment of ch= unsigned (count) - this is taking the integer count and forcing it to be an unsigned integer, then assigning it to ch. The variables a, and b, are also unsigned integers, but when these values are copied (call by value - remember) to the functions notice that the functions believe the arguments are integers.

    Addition is 25
    Subtraction is 5
    Multiplication is 150

     Adapted from:
    "Function Pointer in C" by Abhay Rathi, Geeks for Geeks

    • Was this article helpful?