Here's a simple (and a silly one, yeah) example of what I'm trying to do:
#include <iostream>
void callFunctionsFromAnArray(void *(*)(int), int);
void f1(int);
void f2(int);
void f3(int);
int main()
{
const int n = 3;
void (*functions[n]) (int) = { f1, f2, f3 };
callFunctionsFromAnArray(functions, n);
}
void callFunctionsFromAnArray(void *(*f) (int), int fCount) {
for (int i = 0; i < fCount; i++)
f[i](1);
}
void f1(int a)
{
std::cout << a * 1 << '\n';
}
void f2(int a)
{
std::cout << a * 2 << '\n';
}
void f3(int a)
{
std::cout << a * 3 << '\n';
}
And those are the errors I'm getting when trying to compile:
12:9: error: no matching function for call to 'callFunctionsFromAnArray'
callFunctionsFromAnArray(functions, n);
3:10: note: candidate function not viable: no known conversion from 'void (*[3])(int)' to 'void *(*)(int)' for 1st argument
void callFunctionsFromAnArray(void *(*)(int), int);
17:13: error: subscript of pointer to function type 'void *(int)'
f[i](1);
2 errors generated.
However, if I change argument to be void (*f[3]) (int)
it works. But the problem is, it's not always possible to know array's size before run-time (that's why function has 2nd argument after all). Is there any solution?