Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Function Pointers in C

Introduction

Function pointers are pointers that point to the address of a function. In C, functions are not first-class citizens, but we can use pointers to functions to achieve a similar effect. This allows for dynamic function calls, callback mechanisms, and more flexible code structures.

Declaration and Initialization

To declare a function pointer, you need to specify the return type and parameter types of the function it points to. Below is the syntax for declaring a function pointer:

return_type (*pointer_name)(parameter_types);

For example, if you have a function that takes two integers and returns an integer, the declaration would be:

int (*func_ptr)(int, int);

Assigning a Function to a Function Pointer

Once you have declared a function pointer, you need to assign a function to it. This is done using the address-of operator (&) or directly with the function name:

func_ptr = &function_name;

or

func_ptr = function_name;

Here is an example:

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

Calling a Function Using a Function Pointer

To call a function using its pointer, you use the dereference operator (*) or directly use the function pointer:

result = (*func_ptr)(arg1, arg2);

or

result = func_ptr(arg1, arg2);

Example:

int result = (*func_ptr)(2, 3);
// or
result = func_ptr(2, 3);

Example Program

Here is a complete example demonstrating the usage of function pointers:


#include <stdio.h>

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

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

int main() {
    int (*func_ptr)(int, int);

    // Point to add function
    func_ptr = add;
    printf("Addition: %d\n", func_ptr(10, 5));

    // Point to subtract function
    func_ptr = subtract;
    printf("Subtraction: %d\n", func_ptr(10, 5));

    return 0;
}
                

Output:

Addition: 15

Subtraction: 5

Function Pointers as Arguments

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

Example:


#include <stdio.h>

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

int operate(int (*operation)(int, int), int a, int b) {
    return operation(a, b);
}

int main() {
    printf("Result: %d\n", operate(add, 10, 5));
    return 0;
}
                

Output:

Result: 15

Arrays of Function Pointers

You can also create arrays of function pointers. This is useful for creating a table of functions that can be called based on an index.

Example:


#include <stdio.h>

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

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

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

    printf("Addition: %d\n", operations[0](10, 5));
    printf("Subtraction: %d\n", operations[1](10, 5));

    return 0;
}
                

Output:

Addition: 15

Subtraction: 5

Conclusion

Function pointers are a powerful feature in C that allow for dynamic function calls, callbacks, and more flexible code structures. They can be used in various ways, including passing them as arguments, storing them in arrays, and more. Understanding function pointers can greatly enhance your ability to write modular and dynamic C code.