array_of_functions_in_c
array_of_functions_in_c
In C programming, you can create an array of functions by using function pointers. Function pointers
allow you to store addresses of functions, making it possible to call different functions dynamically.
This is useful for cases where you have multiple functions with similar signatures and want to call
Here is an example of an array of function pointers for functions with 'int' return type:
#include <stdio.h>
int function1() {
printf("Function 1 executed.\n");
return 1;
int function2() {
printf("Function 2 executed.\n");
return 2;
}
int function3() {
printf("Function 3 executed.\n");
return 3;
int main() {
// Call the functions through the array and store the return value
return 0;
In this example:
- We define three functions, 'function1', 'function2', and 'function3', each of which returns an 'int' and
prints a message.
- An array of function pointers 'functionArray' is created, where each pointer points to one of the
three functions.
- The loop iterates through the array, calling each function and storing the return value in 'result'.
- Finally, the return value is printed for each function.
Conclusion
Creating an array of functions in C using function pointers provides a flexible and dynamic way to
manage multiple functions that share the same signature. It allows for easy expansion of code
without modifying the core logic. This technique is especially useful when you need to call a series