Extra 5% OFF Use Code: OL05
Free Shipping over ₹999

Pointers to Functions

A pointer to a function in C allows you to store the address of a function and then call the function indirectly through the pointer. Function pointers are particularly useful in scenarios such as callback functions, implementing function tables, or when you need to pass functions as arguments to other functions.

1. Declaring a Function Pointer

The syntax for declaring a function pointer is:

return_type (*pointer_name)(parameter_types);

For example, if we have a function int add(int, int), a pointer to this function would be declared as:

int (*funcPtr)(int, int);

2. Assigning a Function to a Function Pointer

You can assign the address of a function to a function pointer using the function’s name (without parentheses).

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

int (*funcPtr)(int, int);  // Declare a function pointer
funcPtr = add;              // Assign the address of `add` to `funcPtr`

Now, funcPtr can be used to call add.

3. Calling a Function via a Pointer

Once you have assigned a function to a pointer, you can call the function using the pointer like this:

int result = funcPtr(5, 3);  // Calls add(5, 3) and stores the result

Example of Function Pointer

Here’s an example that demonstrates how to use a function pointer to call different arithmetic operations based on the pointer’s assignment:

#include <stdio.h>

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

int subtract(int a, int b) {
    return a - b;
}

int main() {
    int (*operation)(int, int);  // Declare a function pointer

    // Assign `add` to the function pointer and call it
    operation = add;
    printf("Addition: %d\n", operation(5, 3));

    // Assign `subtract` to the function pointer and call it
    operation = subtract;
    printf("Subtraction: %d\n", operation(5, 3));

    return 0;
}

Explanation:

  • We declare a function pointer operation that can point to functions taking two int arguments and returning an int.
  • First, we set operation to point to add and call operation(5, 3), which returns 8.
  • Then, we set operation to point to subtract and call operation(5, 3), which returns 2.

4. Passing Function Pointers as Arguments

Function pointers can also be passed as arguments to other functions. This is useful in scenarios where a function’s behavior depends on the specific operation passed to it.

Example of Passing Function Pointers as Arguments

#include <stdio.h>

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

int multiply(int a, int b) {
    return a * b;
}

void performOperation(int x, int y, int (*operation)(int, int)) {
    printf("Result: %d\n", operation(x, y));
}

int main() {
    // Call performOperation with different function pointers
    performOperation(5, 3, add);        // Calls add(5, 3)
    performOperation(5, 3, multiply);   // Calls multiply(5, 3)

    return 0;
}

Explanation:

  • performOperation takes two integers and a function pointer as parameters.
  • By passing different functions (add or multiply) to performOperation, we can dynamically choose the operation to perform on x and y.

Summary

  • Function pointers store addresses of functions, allowing indirect function calls.
  • Function pointers can be assigned to different functions and called like normal functions.
  • You can pass function pointers as arguments to other functions, allowing flexibility in function behavior.

    Leave a Reply

    Your email address will not be published.

    Need Help?