A function pointer in C is a pointer that points to a function instead of a data value. It allows you to store the address of a function in a pointer variable and invoke the function via the pointer. Function pointers are particularly useful for implementing callback functions, event-driven programming, or when you need to select between different functions at runtime.
Let’s dive into function pointers in detail, including their syntax, use cases, and examples.
To declare a function pointer, you need to specify the function signature that the pointer will point to. The syntax for declaring a function pointer looks like this:
return_type (*pointer_name)(parameter_types);
Where:
return_type is the return type of the function.pointer_name is the name of the pointer.parameter_types are the types of the parameters that the function takes.Consider a function that adds two integers:
int add(int a, int b) {
return a + b;
}
To declare a pointer to this function:
int (*ptr)(int, int);
This declares ptr as a pointer to a function that takes two int parameters and returns an int. Now, you can assign the address of the add function to ptr:
ptr = add;
Once a function pointer is initialized, you can use it to call the function it points to. The syntax for calling a function through a function pointer is:
(*ptr)(arg1, arg2);
Alternatively, you can use the simpler form:
ptr(arg1, arg2);
#include <stdio.h>
// Function declaration
int add(int a, int b) {
return a + b;
}
int main() {
// Declare a function pointer
int (*ptr)(int, int);
// Point the function pointer to the add function
ptr = add;
// Call the function through the pointer
int result = ptr(5, 3); // Equivalent to add(5, 3)
// Output the result
printf("Result: %d\n", result);
return 0;
}
ptr is assigned the address of the add function.ptr(5, 3), which is equivalent to add(5, 3).8.Result: 8
You can use function pointers to choose between multiple functions dynamically, allowing for greater flexibility and modularity in your code.
#include <stdio.h>
// Function declarations
int add(int a, int b) {
return a + b;
}
int multiply(int a, int b) {
return a * b;
}
int main() {
// Declare a function pointer
int (*operation)(int, int);
// Assign the function pointer to 'add'
operation = add;
printf("Addition: %d\n", operation(5, 3)); // Calls add
// Change the function pointer to 'multiply'
operation = multiply;
printf("Multiplication: %d\n", operation(5, 3)); // Calls multiply
return 0;
}
operation is first assigned to add, and then the add function is called.multiply, and the multiply function is called.Addition: 8
Multiplication: 15
In this example, the function that gets called is determined by which function the operation pointer is pointing to.
Function pointers are often used as callback functions. A callback is a function that is passed as an argument to another function and is invoked within that function.
#include <stdio.h>
// Function declarations
int add(int a, int b) {
return a + b;
}
int multiply(int a, int b) {
return a * b;
}
// Function that takes a function pointer as a parameter
void performOperation(int a, int b, int (*operation)(int, int)) {
int result = operation(a, b); // Call the function via pointer
printf("Result: %d\n", result);
}
int main() {
// Call performOperation with add
performOperation(5, 3, add); // Addition
// Call performOperation with multiply
performOperation(5, 3, multiply); // Multiplication
return 0;
}
performOperation is a function that accepts two integers and a function pointer operation as parameters.add and multiply functions as arguments to performOperation, which then calls the appropriate function through the pointer.Result: 8
Result: 15
This approach is useful when you need to provide flexibility to a function by allowing it to perform different operations based on the function pointer passed to it.
You can store multiple function pointers in an array, which is useful when you need to handle multiple functions in a structured way, such as implementing a simple menu-driven program or selecting functions dynamically.
#include <stdio.h>
// Function declarations
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 divide(int a, int b) {
return a / b;
}
int main() {
// Array of function pointers
int (*operations[4])(int, int) = {add, subtract, multiply, divide};
int a = 10, b = 5;
// Perform all operations using the array of function pointers
printf("Addition: %d\n", operations[0](a, b));
printf("Subtraction: %d\n", operations[1](a, b));
printf("Multiplication: %d\n", operations[2](a, b));
printf("Division: %d\n", operations[3](a, b));
return 0;
}
operations stores function pointers to four different functions: add, subtract, multiply, and divide.a and b as arguments.Addition: 15
Subtraction: 5
Multiplication: 50
Division: 2
In this example, you can easily add more operations to the array, and the code remains flexible and modular.
Function Pointers: Function pointers store the address of a function, allowing you to call the function indirectly.
Calling Functions via Pointers: You can invoke a function through its pointer using the (*pointer)(args) syntax or the simpler pointer(args).
Callbacks: Function pointers allow you to pass functions as arguments, enabling callback mechanisms where one function calls another function dynamically.
Arrays of Function Pointers: You can create arrays of function pointers to manage and call multiple functions in a structured way.
Function pointers are a powerful feature in C that enable dynamic function calls, callbacks, and modular design. They are widely used in applications like event-driven programming, table-driven algorithms, and managing multiple functions in a flexible and extensible manner.
Open this section to load past papers