Advertisement

A Function Pointer in C also known as pointer to a function is a pointer that holds the address of a function. Function Pointer provides users the functionality to call the function at runtime.

In order to understand the function pointer more clearly, you need to get a general understanding of how the function call is stacked in the stack memory. Read the memory layout of the C program to get a basic idea of how the stack memory works.

Visually, it could be hard to differentiate between a function returning a pointer and a function pointer. Let’s make it simple:

Function Returning a Pointer

Here is a function declaration that returns an integer type pointer:

int* sum(int,int);
int *sum(int,int);

As you would have detected from the code above, the asterisk operator can be put just after the data type and before the name of the function.

The precedence of parenthesis is higher than the asterisk operator.

So perhaps we can say that sum is a function with two integer data types as arguments that return a pointer to an integer type.

Function Pointer

Here is a function pointer or a pointer to a function declaration:

int (*ptr)(int,int);

This is a function pointer as the *ptr is wrapped in parenthesis. ptr is a pointer function that accepts two integer data types as arguments and returns an integer value.

Function Pointer Declaration

 

Function Pointer Declaration

Assign Address to Function Pointer

#include 
int sum(int a, int b)
{
    return a + b;
}

int main()
{
    int (*ptr)(int, int) = ∑
    printf("%d\n", (*ptr)(10, 20));
    return 0;
}

Output:- 30

Explanation of the code above:

There is a function called sum that accepts two integer data types and returns the sum of the passed arguments.

In the main function, there is a function pointer declaration int (*ptr)(int, int) which holds the address of the function sum &sum.

Next line, the function pointer is called (*ptr)(10, 20) and the value is printed using the printf function.

Function Address

Just like an array, functions return their address when the function name is used.

int main()
{
    printf("%d\n", sum);
    return 0;
}

So, there is no need to pass the address of function using the ampersand & operator.

After re-factoring the previous code:

#include 
int sum(int a, int b)
{
    return a + b;
}

int main()
{
    int (*ptr)(int, int) = sum;
    printf("%d\n", ptr(10, 20));
    return 0;
}

Function Pointer Comparision

Function pointers can also be compared to one another using the equality and inequality operator.

int main()
{
    int (*ptr)(int, int) = sum;
    if (sum == ptr)
    {
        printf("ptr points to the sum function\n");
    }
    else
    {
        printf("ptr points doesn't point to the sum function\n");
    }
}

Output:- ptr points to the sum function

As the ptr function pointer holds the address of the sum function and the sum function name returns its address, the equality condition is satisfied.

Passing & Returning Function Pointers

Like normal pointers, a function pointer can also be passed to a function as an argument and a function can also return function pointers.

Passing a function pointer:

#include 

typedef int (*fptr)(int, int);

int sum(int a, int b)
{
    return a + b;
}

int compute(fptr operation, int a, int b)
{
    return operation(a, b);
}

int main()
{
    printf("%d\n", compute(sum, 10, 20));
    return 0;
}

In the above code, the compute function accepts an operation of type fptr (typedef of a function pointer).

Returning a function Pointer:

typedef int (*fptr)(int, int);


fptr operation(char ch)
{
    switch (ch)
    {
        case '+': return sum;
        case '-': return subtract;
        case '*': return multiply;
        case '%': return divide;
    }
}

An array of function pointers

Just like normal pointers where an array of pointers is possible, an array of function pointers is also possible.

An array of function pointers stores the address of each function.

int main()
{
    fptr arr[] = {sum, subtract, multiply, divide};
    printf("Sum = %d\n", (*arr[0])(10, 20));
    printf("Subtract = %d\n", (*arr[1])(20, 10));
    printf("Multiply = %d\n", (*arr[2])(10, 20));
    printf("Divide = %d\n", (*arr[3])(20, 10));
    return 0;
}

Sum = 30
Subtract = 10
Multiply = 200
Divide = 2

The function pointer arrays may be used to select a function to be evaluated on the basis of certain conditions.

Application of Function Pointer

Callback Function

Function pointers are helpful when the programmer needs to implement a callback feature in the code. The callback can be accomplished by simply passing the address of the function, and the receiving function can call it as required.

Example:

The qsort function in c language accepts a callback function i.e address of a function.

int compare(const void *a, const void *b)
{
    int l = *(const int *)a;
    int r = *(const int *)b;
    return l > r;
}

int main()
{
    int arr[5] = {2, 3, 5, 4, 1};

    qsort(arr, 5, sizeof(arr[0]), compare);

    for (int i = 0; i < 5; i++)
    {
        printf("%d ", arr[i]);
    }
}

Dynamic Function Call

Function Pointer also allows the dynamic execution of functions. An array of function pointers holds the address of each function pointer and is called when the programmer feels the need.

#include 

typedef int (*fptr)(int, int);

int sum(int a, int b)
{
    return a + b;
}
int subtract(int a, int b)
{
    return a - b;
}
int multiply(int a, int b)
{
    return a * b;
}
int divide(int a, int b)
{
    return a / b;
}

int main()
{
    int a, b;
    char ch;
    printf("Perform Calculation:\n");
    scanf("%d %c %d", &a, &ch, &b);
    fptr arr[128];
    arr['+'] = sum;
    arr['-'] = subtract;
    arr['*'] = multiply;
    arr['%'] = divide;
    printf("=> %d\n", (*arr[ch])(a, b));
    return 0;
}

$ ./calculate.exe
Perform Calculation:
10 + 20
=> 30
$ ./calculate.exe
Perform Calculation:
20 % 2
=> 10
$ ./calculate.exe
Perform Calculation:
30 - 10
=> 20
$ ./calculate.exe
Perform Calculation:
2 * 25
=> 50

About Author
5 3 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
Scroll to Top