C Programs Tutorials | IT Developer
IT Developer

C Programming - C Function Pointers



Share with a Friend

C Programming - C Function Pointers

C Function Pointers

A function pointer in C is a pointer that points to the address of a function. This allows functions to be passed as arguments to other functions, enabling dynamic function calls, creating callback mechanisms, and implementing function tables (useful for event-driven programming, etc.).

Function pointers are extremely powerful because they allow programs to select a function at runtime instead of at compile-time, providing flexibility and extensibility in code design.

Syntax of Function Pointers:

To declare a function pointer, you must specify the return type and the parameters of the function the pointer will point to. The general syntax for declaring a function pointer is:

C

return_type (*pointer_name)(parameter_types);

  • return_type: The return type of the function the pointer points to.
  • pointer_name: The name of the function pointer.
  • parameter_types: The types of parameters that the function accepts.

Example 1: Basic Function Pointer

Let’s start with a simple example that demonstrates how to declare and use function pointers.

C

    

#include <stdio.h>

// Function to add two integers

int add(int a, int b) {

    return a + b;

}

// Function to subtract two integers

int subtract(int a, int b) {

    return a - b;

}

int main() {

    // Declare a function pointer and assign it to the 'add' function

    int (*func_ptr)(int, int);

    // Point to the 'add' function

    func_ptr = add;

    printf("Addition: %d\n", func_ptr(10, 5));  // Calls add(10, 5)

    // Point to the 'subtract' function

    func_ptr = subtract;

    printf("Subtraction: %d\n", func_ptr(10, 5));  // Calls subtract(10, 5)

    return 0;

}

Output:

Addition: 15

Subtraction: 5

In this example:

  • func_ptr is a function pointer that points to functions accepting two int parameters and returning an int.
  • First, func_ptr points to the add function, and then we call it with the arguments 10 and 5.
  • Later, we change the pointer to point to the subtract function and call it in the same way.

Example 2: Function Pointer as a Function Argument

Function pointers can be passed as arguments to other functions. This is useful for implementing callback functions.

C

#include <stdio.h>

// Function that performs an operation on two integers using a function pointer

int performOperation(int a, int b, int (*operation)(int, int)) {

    return operation(a, b);  // Calls the function pointed to by 'operation'

}

// Function to multiply two integers

int multiply(int a, int b) {

    return a * b;

}

// Function to add two integers

int add(int a, int b) {

    return a + b;

}

int main() {

    int result;

    // Using function pointer to pass 'add' function

    result = performOperation(5, 3, add);

    printf("Addition Result: %d\n", result);

    // Using function pointer to pass 'multiply' function

    result = performOperation(5, 3, multiply);

    printf("Multiplication Result: %d\n", result);

    return 0;

}

Output:

Addition Result: 8

Multiplication Result: 15

In this example:

  • performOperation takes two integers and a function pointer operation, which points to a function that operates on the integers.
  • The function pointer allows performOperation to be used with different functions (like add and multiply).

Example 3: Array of Function Pointers

You can create an array of function pointers to handle multiple functions that share the same signature. This is useful when you want to select and execute a function dynamically, such as in a menu system.

C

#include <stdio.h>

// Function to add two integers

int add(int a, int b) {

    return a + b;

}

// Function to subtract two integers

int subtract(int a, int b) {

    return a - b;

}

// Function to multiply two integers

int multiply(int a, int b) {

    return a * b;

}

int main() {

    // Array of function pointers

    int (*operations[])(int, int) = {add, subtract, multiply};

    int a = 10, b = 5;

    // Call each function in the array

    printf("Addition: %d\n", operations[0](a, b));   // Calls add(10, 5)

    printf("Subtraction: %d\n", operations[1](a, b)); // Calls subtract(10, 5)

    printf("Multiplication: %d\n", operations[2](a, b)); // Calls multiply(10, 5)

    return 0;

}

Output:

Addition: 15

Subtraction: 5

Multiplication: 50

In this example:

  • We declare an array operations[] of function pointers, where each pointer points to a function (add, subtract, multiply).
  • Each function is called by indexing into the array of pointers.

Example 4: Returning a Function Pointer

Functions can also return a function pointer, which can be used for dynamic function selection.

C

#include <stdio.h>

// Function to add two integers

int add(int a, int b) {

    return a + b;

}

// Function to return a pointer to the add function

int (*getAddPointer())(int, int) {

    return add;  // Returns a pointer to the add function

}

int main() {

    int (*ptr)(int, int) = getAddPointer();  // Get pointer to 'add' function

    printf("Result of addition: %d\n", ptr(5, 3));  // Calls add(5, 3) through pointer

    return 0;

}

Output:

Result of addition: 8

In this example:

  • The function getAddPointer returns a pointer to the add function.
  • The returned pointer is assigned to ptr, and we use it to call the add function indirectly.

Advantages of Function Pointers:

  1. Flexibility: You can select which function to call at runtime.
  2. Callback Mechanism: Function pointers are commonly used in callback functions to allow functions to be passed as arguments (e.g., sorting algorithms).
  3. Event-driven Programming: Function pointers are useful in event-driven systems like GUI applications, where functions are triggered by events.

Conclusion:

Function pointers are a powerful feature in C that enable dynamic function calls, flexible code, and can be used for callbacks, event handling, and more. They allow functions to be passed around, stored, and invoked at runtime, giving C programs significant flexibility. However, you should handle them carefully, as incorrect use of function pointers can lead to undefined behavior or memory issues.