Pointer to an array of pointers to functions

Hello there. I just start with the code of the problem I hinted in the title:

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
#include <iostream>

using namespace std;

void my_int_func_a(int x)
{
    cout << "Peep: " << x << "\n";
}

void my_int_func_b(int x)
{
    cout << "Poop: " << x << "\n";
}

int main()
{
    /*works*/
    void (*bar[2])(int);

    bar[0] = my_int_func_a;
    bar[1] = my_int_func_b;

    void (*(*bar_p)[2])(int);
    bar_p = &bar;
    (*(bar_p))[0](1.1);
    (*(bar_p))[1](2.2);

    /*crashes*/
    /*void (*(*foo)[2])(int);

    (*(foo))[0] = my_int_func_a;
    (*(foo))[1] = my_int_func_b;

    (*(foo))[0](1.1);
    (*(foo))[1](2.2);*/

}



I use code::blocks on Windows 7. Without any compiler-flags it compiles and gets me a Runtime-Error.

When I set the "-Werror"-Compiler-Flag I get this error:
"'foo' is used uninitialized in this function"

I started some days ago to read the good, old "Thinking in C++" Volume one and reached the listing about complicated definitions yesterday (page 199 in my book). While I tried to write a definition for f4 I ran into this problem.

Do you guys know what I am doing wrong?

Thanks in advance,
byte a bit
Last edited on
When I set the "-Werror"-Compiler-Flag I get this error:
"'foo' is used uninitialized in this function"


foo is a pointer - you need to give it a valid address of an object of the appropriate type before you try to dereference it.

Wooops....

Thanks, now it seems so obvious.
Topic archived. No new replies allowed.