(code examples haven’t been tested – just are for example)
I have several declarations like so:
void (* const a[])(void) = {func1, func2};
void func1() {};
void func2() {};
a[0]();
That compiles (if it was real code…) and the should runs func1.
However, if I want to pass arguments such as:
a[0](int 10);
Then I change my declarations:
void (* const a[])(int) = {func1, func2};
void func1(int foo) {};
void func2(int foo) {};
a[0](10);
That also compiles but I get the following warning:
warning: initialisation from incompatible pointer type.
The resulting code also runs fine, but I suspect that the declaration is at fault.
I can find several examples on how to build function tables that do not pass parameters but am struggling to find an example that shows how to do it with one.
Most likely the declarations of
func1andfunc2declare them as takingvoidas a parameter, rather than takingint. You probably havein some header file. Instead, you need
This is because the declaration
means that
ais an array of function pointers takingintand returningvoid.