Array of Functions?

Hello, I have been trying to create an array of (void) function pointers but so far to no avail. I'm not too sure how to create an array function pointers. The only thing I would like to do after I can do that is to randomly select a function from that array. If this is possible, please attach a sample of the code that does this.

Cheers,
Harry
To declare an array of function pointers: void (*array[size])();
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#define ARRAYSIZE(ar)   (sizeof(ar) / sizeof(*(ar)))

typedef void (* VoidFunc)(int i);

static void f(int i);
static void g(int i);
static void h(int i);

/* ===================================== */

static VoidFunc voidFuncs[] =
{
    f,
    g,
    h
};

/* ===================================== */

static void f(int i)
{
    cout << "f(" << i << ")" << endl;

} /* f() */

static void g(int i)
{
    cout << "g(" << i << ")" << endl;

} /* g() */

static void h(int i)
{
    cout << "h(" << i << ")" << endl;

} /* h() */

/* ===================================== */

int main()
{
    for (unsigned int i = 0; i < ARRAYSIZE(voidFuncs); i++)
    {
        (*voidFuncs)(i);
    }

    return 0;

} /* main() */

Topic archived. No new replies allowed.