Steve said:
Can anyone tell me if I can have an array of functions that take a variable
number of parameters? If it is possible I'd like to know how to declare the
array and the functions as its elements. I am looking for something like
this:
array[] = {func1(a, b, c), func2(b, c), func3(a), func4(a,b,c,d)....}
If I need to call a function I want to be able to call the function by
writing something like array[3]
Is that or anything close to that format possible?
If all the functions have the same return type you should be able to
create an array of function pointers, all returning the same type and
with an unspecified number of arguments, e.g. after a declaration of
the functions
int func1( int a, double b, long c );
int func2( int a, double b );
int func3( int a, double b, long c, double d );
you would have the definition of the array
int ( *func_array[ ] )( ) = { func1, func2, func3 };
Then the functions could be called like this
int x;
....
x = func_array[ 0 ]( a, b, c );
etc. But note that the function arguments then are, since their types
aren't specified, subject to integral promotion and float arguments
are converted to double before a function is called.
If the functions don't even have all the same return type things are
getting much more complictated. In the assignment to the array they
would have to be cast to a common type and then recast to their real
type before using them in a call. But that wouldn't fit the way you
want to call them since casting is a compile time concept and can't
be done dynamically, i.e. you would already need to know at compile
time the function (or at least its type) that will be called. The
only way around this I can see at the moment would be to use an
array of unions of function pointers instead of an array of function
pointers, where the union is something like
union func_types {
void ( *void_func )( );
int ( *int_func )( );
double ( *double_func )( );
....
};
and in the function call select the correct member of the array element
- but that's probably going to be tricky and ugly.
Regards, Jens