Reputation: 715
What are the benefits of using array of function pointers ?? Other than reducing the number of function prototypes in C .
Upvotes: 3
Views: 426
Reputation: 14467
As many people noted, function pointers can be used to emulate the object-oriented programming in C.
For example, the selection of the appropriate method (a.k.a. virtual methods) can be done using simple indexing.
#include <stdio.h>
#include <stdlib.h>
typedef void (*menu_function_t)();
void menu_error()
{
printf("Invalid menu choice\n");
}
void menu_exit()
{
exit(0);
}
void menu_function1()
{
printf("You have selected 1\n");
}
void menu_function2()
{
printf("You have selected 2\n");
}
menu_function_t menu_ptr[] = {
menu_function1, menu_function2, menu_exit, menu_error
};
int menu_num = sizeof(menu_ptr) / sizeof(menu_ptr[0]);
int validate_choice(int a)
{
if(a < 0 || a > menu_num - 2) { return 3; }
return a;
}
int main()
{
int choice = 0;
while(1)
{
printf("Choose item: \n");
printf("0) Function 1\n");
printf("1) Function 2\n");
printf("2) Exit\n");
scanf("%d", &choice);
menu_ptr[ validate_choice(choice) ]();
}
return 0;
}
Upvotes: 3
Reputation: 9278
If you wanted to associate a numeric value with an operation you could use an array of function pointers, although it'd really be called a map. It'd save having a big switch
statement.
array[CMD_OPEN] = OpenFunction;
array[CMD_CLOSE] = CloseFunction;
etc
void HandleCommand(int aCommand)
{
array[aCommand]();
}
Upvotes: 2
Reputation: 57388
One example (many exist) is when you want to implement a series of variable "methods":
typedef struct tag_IMAGEFORMAT
{
struct tag_IMAGEFORMAT *next;
const char *name;
IMAGEDETECTOR *detect_routine;
int (*open_routine) (DISKIMAGE *img, diskimgflags_t flags);
size_t (*read_routine) (DISKIMAGE *img);
int (*close_routine) (DISKIMAGE *img);
} IMAGEFORMAT;
Object-oriented programming in C. Then you call e.g. object->open_routine(..)
, whose implementation can be changed dynamically at runtime (in this example, depending on image format).
In this example several image formats share a common API; the various "detectors" get run one after the other, and the first matching one copies its pointers to the "generic image" object. After which, you can manipulate an image without worrying about its implementation details (JPG, GIF, PNG, ...).
Similarly if you need to implement several different variations of a function (e.g. an image filter), you could keep all pointers into an array, and choose the appropriate one through an index into the array.
Upvotes: 1
Reputation: 150108
There are a variety of use cases for arrays (or other data structures) holding function pointers. Generally, that type of mechanism allows your program to dynamically call one of several different functions based on runtime criteria.
For example, you could implement something similar to interfaces from OO languages using an array of function pointers. The array would hold a pointer to each implementation of that interface. The array index would be used to select the desired implementation, based on some criteria.
Upvotes: 2