Function Pointer as Argument in C

In the C programming language, a function pointer is a variable that can store the address of a function. This allows you to pass functions as arguments to other functions, which can be a powerful tool for creating flexible and modular code. In this article, we will explore how to use function pointers as arguments in C.

Declaring a Function Pointer

To declare a function pointer, you need to specify the return type and the parameter types of the function that the pointer will point to. The syntax for declaring a function pointer is as follows:

return_type (*pointer_name)(parameter_types);

For example, to declare a function pointer named myFuncPtr that points to a function with no parameters and returns an int, you would use the following declaration:

int (*myFuncPtr)();

Passing a Function Pointer as an Argument

Once you have declared a function pointer, you can pass it as an argument to another function. This allows the receiving function to call the function pointed to by the pointer. Here is an example:

void callFunction(int (*funcPtr)()) {
    int result = funcPtr();
    printf("Result: %dn", result);
}

int myFunction() {
    return 42;
}

int main() {
    callFunction(myFunction);
    return 0;
}

In this example, we have a function named callFunction that takes a function pointer as an argument. Inside callFunction, we call the function pointed to by the pointer and print the result. We also have a function named myFunction that returns the value 42. In the main function, we call callFunction with myFunction as the argument.

When we run this program, it will output:

Result: 42

As you can see, the callFunction function successfully calls the myFunction function through the function pointer.

Using Function Pointers for Callbacks

One common use case for function pointers as arguments is to implement callbacks. A callback is a function that is passed as an argument to another function and is called by that function at a later point in time. This allows you to customize the behavior of a function without modifying its code. Here is an example:

void performOperation(int a, int b, int (*operation)(int, int)) {
    int result = operation(a, b);
    printf("Result: %dn", result);
}

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

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

int main() {
    performOperation(5, 3, add);
    performOperation(5, 3, subtract);
    return 0;
}

In this example, we have a function named performOperation that takes two integers and a function pointer as arguments. Inside performOperation, we call the function pointed to by the pointer with the two integers as arguments and print the result. We also have two functions named add and subtract that perform addition and subtraction, respectively. In the main function, we call performOperation twice, once with add and once with subtract, as the third argument.

When we run this program, it will output:

Result: 8
Result: 2

As you can see, the performOperation function successfully calls the add and subtract functions through the function pointer, allowing us to perform different operations with the same code.

Using function pointers as arguments in C allows you to create more flexible and modular code. It enables you to pass functions as arguments to other functions, which can be useful for implementing callbacks and customizing function behavior. By understanding how to declare and use function pointers as arguments, you can take advantage of this powerful feature of the C programming language.

Scroll to Top