C Function Pointers

A function pointer is a variable that stores the address of a function rather than a data value.

If you need to work with functions dynamically in C, function pointers allow you to store and invoke functions using their addresses. They are useful for callbacks, event handling, and implementing flexible code architectures.


Understanding Function Pointers

The syntax for declaring a function pointer is:

</>
Copy
return_type (*pointer_name)(parameter_types);

Explanation:

  1. return_type: The type of value the function returns.
  2. (*pointer_name): The name of the function pointer variable; the asterisk indicates it is a pointer.
  3. (parameter_types): A comma-separated list of the parameter types the function accepts.

Example 1: Basic Function Pointer Usage

This example demonstrates how to create a function pointer to a simple function that adds two integers and then call the function through the pointer.

main.c

</>
Copy
#include <stdio.h>

// A simple function that adds two integers
int add(int a, int b) {
    return a + b;
}

int main() {
    // Declare a function pointer that points to a function taking two ints and returning an int
    int (*funcPtr)(int, int) = add;
    
    // Call the function using the function pointer
    int result = funcPtr(10, 20);
    printf("Result of add(10, 20): %d\n", result);
    
    return 0;
}

Explanation:

  1. int add(int a, int b): Defines a function that adds two integers.
  2. int (*funcPtr)(int, int) = add;: Declares a function pointer named funcPtr that points to a function with the same signature as add, and initializes it with the address of add.
  3. funcPtr(10, 20): Calls the add function via the function pointer with arguments 10 and 20.
  4. printf(): Prints the result returned by the function call.

Output:

Result of add(10, 20): 30

Example 2: Using an Array of Function Pointers

This example shows how to store multiple functions in an array of function pointers and invoke them based on their index. This technique is useful when you want to select a function to execute at runtime.

main.c

</>
Copy
#include <stdio.h>

// Function definitions
int add(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 main() {
    // Array of function pointers
    int (*operations[3])(int, int) = { add, subtract, multiply };
    
    int x = 15, y = 5;
    
    // Call each function using the function pointer array
    printf("Add: %d\n", operations[0](x, y));
    printf("Subtract: %d\n", operations[1](x, y));
    printf("Multiply: %d\n", operations[2](x, y));
    
    return 0;
}

Explanation:

  1. Three functions (add, subtract, and multiply) are defined, each taking two integers and returning an integer.
  2. An array operations of function pointers is declared; each element is capable of pointing to a function that matches the signature (int, int) -> int.
  3. The array is initialized with the addresses of the three functions.
  4. The functions are invoked by calling the corresponding element in the array with the arguments x and y.
  5. printf() prints the results of each function call.

Output:

Add: 20
Subtract: 10
Multiply: 75

Conclusion

In this tutorial, we learned about C function pointers, including:

  1. What Function Pointers Are: Variables that store the address of functions.
  2. Basic Usage: Declaring, initializing, and calling a function using a pointer.
  3. Array of Function Pointers: Organizing multiple function pointers in an array to dynamically choose which function to execute.