The solution is different in C and in C++, and also different in
C++11 and in pre-C++11.
In all of these, you can have a simple array of pointers to
functions:
double (*array[3])() = { &f1, &f2, &f3 };
To call a function:
std::cout << (*array[i])() << std::endl;
This is very limiting, since it means that the functions in
question cannot depend on any other data. For this reason, it
is usual in C to create a struct:
struct F
{
double (*pf)( void* );
void* data;
};
F array[3] = { { &f1, NULL }, { &f2, someData }, { &f3, NULL } };
std::cout << (*array[i].pf)( &data ) << std::endl;
(There are, however, a lot of cases, particularly in numerical
calculation, where this is overkill. It's mostly used by
callbacks.)
In C++, you also have the option of defining an abstract base
class, with a virtual function (which will be overridden in each
derived class), and saving pointers to instances of various
derived classes:
class F
{
public:
virtual ~F() {}
virtual double operator()() const = 0;
};
F const* array[3] = { &obj1, &obj2, &obj3 };
std::cout<< (*array[i])() << std::endl;
Finally, in C++11, there is a standard object std::function
which encapsulates all of this. (You'll have to look this up
yourself, however. Like most people, I don't yet have access to
a compiler which fully supports C++11, so have been unable to
practice this.)