3

What are the benefits of using array of function pointers ?? Other than reducing the number of function prototypes in C .

StrawhatLuffy
  • 695
  • 1
  • 7
  • 17
  • 6
    Your question is pretty vague. Can you clarify what you're trying to ask? – Carl Norum Sep 23 '12 at 21:21
  • 2
    Vague question, and imho your assumption about reducing the number of function prototypes is wrong. – Borgleader Sep 23 '12 at 21:21
  • I suggest you read this for the uses of function pointers: http://stackoverflow.com/questions/1758564/what-are-function-pointers-used-for-and-how-would-i-use-them as well as this: http://www.eskimo.com/~scs/cclass/int/sx10b.html An array of them would IMO, just be useful to organize them together, as well as possibly allowing you to use them in a loop with an iterator. – Rivasa Sep 23 '12 at 21:28

4 Answers4

3

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;
}
Viktor Latypov
  • 14,289
  • 3
  • 40
  • 55
2

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.

Eric J.
  • 147,927
  • 63
  • 340
  • 553
2

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]();
}
James
  • 9,064
  • 3
  • 31
  • 49
1

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.

LSerni
  • 55,617
  • 10
  • 65
  • 107