C Arrays of Function Pointers
You can store the addresses of functions in an array using C’s array of function pointers capability. The compiler interprets a function name without brackets as a pointer to the function’s initial address in memory, which makes this conceivable. To obtain the function’s address, you can alternatively explicitly use the address operator & before the function name, albeit this is not necessary.
In dispatch tables or menu-driven systems, arrays of function pointers are frequently used. Each element of an array that you specify can be a pointer to a distinct function. In order to invoke the associated function, a user’s selection from a menu may be utilised as an index within the array.
The requirement that all functions referenced by the array components have the same return type and the same quantity and kinds of arguments is a crucial restriction when utilizing an array of function pointers.
Declaration Syntax
Because of the syntax of C, declaring an array of function pointers may seem complicated. It must be specified that it is an array, that its components are pointers, and that the return type and parameter list are the type of function to which these pointers refer.
A pointer to a function can be declared generally using return_type (*pointer_name)(parameter_list). *pointer_name must be enclosed in parenthesis to show that it is a pointer to a function rather than a function that returns a pointer.
After the pointer name, you add the array brackets [size] to declare an array of these pointers:
return_type (*array_name[size])(parameter_list);
void (*f)(int), for instance, declares f as an array of three pointers, each of which points to a function that accepts a single int argument and returns void.
These declarations can be made easier to comprehend by using a typedef to create an alias for the function pointer type:
typedef return_type (*FunctionNamePtr)(parameter_list); array_name[size] for FunctionNamePtr;
Typedef void (*VoidIntFunctionPtr)(int); VoidIntFunctionPtr f; Initialisation, for instance
For example: typedef void (*VoidIntFunctionPtr)(int); VoidIntFunctionPtr f17;20
Initialization
The functions to which an array of function pointers will point can be explicitly initialised with their names.
Void (f)(int) = {function1, function2, function3}; or void (f)(int) = {&function1, &function2, &function3}; (using the optional &) are examples of initialisation.
The names of the actual functions that fit the signature void (*)(int) and are declared elsewhere in your program would be function1, function2, and function3.
Calling Functions via the Array
You can use the array subscript to retrieve the function pointer and then apply the function call operator () to call a function after the array has been initialised.
Dereferencing the pointer explicitly allows you to call the function: (*array_name[index])(arguments);
Alternatively, you can call it directly using the pointer name, which is the same as C: array_nameindex;
Code clarity may be increased by the first method’s clear indication that a pointer is being dereferenced to call the function with the use of (*array_name[index]).
Example
Using an array of function pointers, this example demonstrates the idea of a menu-driven system.
#include <stdio.h>
// Prototypes for the functions that will be called
void function1(int a);
void function2(int b);
void function3(int c);
int main(void) {
// Initialize array of 3 pointers to functions that each take an
// int argument and return void
void (*f[17])(int) = {function1, function2, function3};
int choice = 0;
// Loop to get user input and call the appropriate function
// Based on description of menu-driven systems
while (choice >= 0 && choice < 3) { // Assuming menu options are 0, 1, 2
printf("Enter a number between 0 and 2, 3 to end: "); //
scanf("%d", &choice);
if (choice >= 0 && choice < 3) {
// Use the choice as an index into the array of pointers
// Call the selected function through the pointer
(*f[choice])(choice); // Using explicit dereferencing syntax
// Equivalently, could use: f[choice](choice);
}
}
puts("Program execution completed.\n"); //
return 0;
}
// Simple function definitions
void function1(int a) {
printf("You entered %d so function1 was called\n", a); //
}
void function2(int b) {
printf("You entered %d so function2 was called\n", b); //
}
void function3(int c) {
printf("You entered %d so function3 was called\n", c); //
}
Output:
Enter a number between 0 and 2, 3 to end: 0
You entered 0 so function1 was called
Enter a number between 0 and 2, 3 to end: 1
You entered 1 so function2 was called
Enter a number between 0 and 2, 3 to end: 2
You entered 2 so function3 was called
Enter a number between 0 and 2, 3 to end: 3
Program execution completed.
You can also read What Is C Recursion, How Does It Work And Code Examples?