Function Pointers in C: A Guide to Dynamic Function Calls
Explore the concept of function pointers in C, which are variables that store the address of functions, allowing for dynamic function calls. Function pointers are crucial for implementing callback functions and enhance the flexibility of your code. Learn how to declare a function pointer by specifying the return type, pointer name, and parameter types to effectively pass functions as arguments or return them from other functions.
Function Pointers in C
A function pointer in C is a variable that stores the address of a function, just like a regular pointer stores the address of a variable. Function pointers are particularly useful for calling functions dynamically and are essential for implementing callback functions in C.
Function pointers can point to code just like normal pointers, and you can use the function's name to retrieve its address. Functions can also be passed as arguments or returned from other functions.
Declaring a Function Pointer
To declare a function pointer in C, you need to write a declaration statement that includes the return type, the pointer's name, and the parameter types of the function it points to.
Declaration Syntax
The syntax for declaring a function pointer is as follows:
Syntax
function_return_type(*Pointer_name)(function argument list)
Example
Here is a simple hello()
function in C:
Code Example
void hello(){
printf("Hello World");
}
We declare a pointer to this function as follows:
Code Example
void (*ptr)() = &hello;
Now, we can call the function using the function pointer:
Code Example
(*ptr)();
Function Pointer Example
The following example demonstrates how to declare and use a function pointer to call a function:
Code Example
#include <stdio.h>
// Defining a function
void hello() { printf("Hello World"); }
// The main code
int main() {
// Declaring a function pointer
void (*ptr)() = &hello;
// Calling function using the function pointer
(*ptr)();
return 0;
}
Output
Hello World
Note: Unlike normal pointers, which are data pointers, a function pointer points to executable code. You can also declare the pointer as follows:
Code Example
void (*ptr)() = hello;
Function Pointer with Arguments
You can also declare a function pointer for functions with arguments. When declaring the function pointer, you must specify the data types for the parameters.
Suppose we have a function called addition()
that takes two integer arguments:
Code Example
int addition(int a, int b) {
return a + b;
}
To declare a function pointer for the addition()
function, we can do the following:
Code Example
int (*ptr)(int, int) = addition;
We can then call the function through its pointer by passing the required arguments:
Code Example
int z = (*ptr)(x, y);
Example of Function Pointer with Arguments
Here is the complete code demonstrating how to call a function through its pointer:
Code Example
#include <stdio.h>
int addition(int a, int b) {
return a + b;
}
int main() {
int (*ptr)(int, int) = addition;
int x = 10, y = 20;
int z = (*ptr)(x, y);
printf("Addition of x: %d and y: %d = %d", x, y, z);
return 0;
}
Output
Addition of x: 10 and y: 20 = 30
Pointer to Function with Pointer Arguments
You can also declare a function pointer for functions that themselves take pointer arguments. Here’s an example:
Code Example
void swap(int *a, int *b) {
int c;
c = *a;
*a = *b;
*b = c;
}
We can declare a function pointer for the swap()
function as follows:
Code Example
void (*ptr)(int *, int *) = swap;
To swap the values of x
and y
, we can pass their pointers to the function pointer:
Code Example
(*ptr)(&x, &y);
Example of Pointer to Function with Pointer Arguments
Here is the complete code demonstrating this concept:
Code Example
#include <stdio.h>
void swap(int *a, int *b) {
int c;
c = *a;
*a = *b;
*b = c;
}
int main() {
void (*ptr)(int *, int *) = swap;
int x = 10, y = 20;
printf("Values of x: %d and y: %d before swap\n", x, y);
(*ptr)(&x, &y);
printf("Values of x: %d and y: %d after swap", x, y);
return 0;
}
Output
Values of x: 10 and y: 20 before swap
Values of x: 20 and y: 10 after swap
Array of Function Pointers
You can also declare an array of function pointers using the following syntax:
Syntax
type (*ptr[])(args) = {fun1, fun2, ...};
Example of Array of Function Pointers
This property allows you to dynamically call functions through pointers instead of using if-else or switch-case statements. Below is an example:
Code Example
#include <stdio.h>
float add(int a, int b) {
return a + b;
}
float subtract(int a, int b) {
return a - b;
}
float multiply(int a, int b) {
return a * b;
}
float divide(int a, int b) {
return a / b;
}
int main() {
float (*ptr[])(int, int) = {add, subtract, multiply, divide};
int a = 15, b = 10;
// 1 for addition, 2 for subtraction
// 3 for multiplication, 4 for division
int op = 3;
if (op > 4) return 0;
printf("Result: %.2f", (*ptr[op-1])(a, b));
return 0;
}
Output
Result: 150.00
Change the value of the op
variable to 1, 2, or 4 to see the results of other functions.