array of functions

S

Steve

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?
 
E

Emmanuel Delahaye

Steve wrote on 03/09/04 :
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?

You can use the empty parens to signify that the number and types of
the parameters are undefined. Note that the returned type must be the
same (int is recommended)


int func1(FILE *a, char *b, double c);
int func2(int b, double c);
int func3(char *a);
int func4(int a, char *b, double c, FILE *d);

typedef int generic_f ();

generic_f *array[] =
{
func1,
func2,
func3,
func4,
};

This practice is somehow dangerous, because there is no way to check
the good use of the parameters.

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"C is a sharp tool"
 
S

Shanmuhanathan T

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?
You could use function pointers.
K&R2 has the details.
On the web, http://newty.de/fpt/index.html
is good, though it has bits of C++ inbetween.
 
E

Eric Sosman

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?

You cannot have an array of functions: a function is
not a data object. You can, however, have an array of
pointers to functions. This example uses a typedef for
clarity; it could be written without the typedef:

/* A pointer to a function that takes an `int' and
* a `double' as arguments and returns an `int' value:
*/
typedef int (*Fptr)(int, double);

/* Some functions of that type:
*/
int f(int a, double b) { /* code */ }
int g(int u, double v) { /* code */ }
int h(int x, double y) { /* code */ }

/* An array of pointers to those functions:
*/
Fptr array[] = { f, g, h };

/* Calling the i'th of those functions:
*/
result = array(42, 3.14);

You're not out of the woods yet, though. Observe that
all the callled functions have the same type: they all take
an `int' and a `double' and they all return an `int'. If
you need to fill array[] with pointers to functions with
different "signatures," you need to do two things: in the
array[] initialization you must cast each function name to
the `Fptr' type, and when making the call you must first
cast array back to the actual type of the function it
points to (which means you need to keep track of that type
somehow) and provide the correct number and type of arguments.

This sort of thing can be done, but if the number of
different "signatures" is large it becomes unwieldy. It
might be better to take a step back and consider whether
there might be another way to solve the larger problem than
to struggle with arrays of function pointers. What are you
trying to do?
 
J

Jens.Toerring

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
 
O

Old Wolf

Emmanuel Delahaye said:
Can anyone tell me if I can have an array of functions that take
a variable number of parameters?

int func1(FILE *a, char *b, double c);
int func2(int b, double c);
int func3(char *a);
int func4(int a, char *b, double c, FILE *d);

typedef int generic_f ();

generic_f *array[] =
{
func1,
func2,
func3,
func4,
};

This is a rather weak solution, as for some functions it is not even
possible to call them properly, eg.

int func5(short a);

When called as array[4](f), or even array[4]((short)f), the
parameter is promoted to int, but the actual function will
expect a short. The same goes for 'char' and 'float' and
their signed/unsigned forms.

This is the same reason that it is not recommended to mix
prototyped functions with K&R-style functions in the same project.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,146
Messages
2,570,832
Members
47,374
Latest member
anuragag27

Latest Threads

Top