array of pointers to functions

Lloyd Kremer kremer at cs.odu.edu
Wed Jun 14 00:29:34 AEST 1989



>The only
>way I can think of to conveniently pass an unspecified number of
>pointers to functions is to construct an array of pointers to the
>individual functions.  Imagine my alarm at discovering that this
>is not legal.


The usual way to pass a variable number of arguments to a function is to
use the varargs or stdarg mechanisms with an argument list consisting of
pointers to functions, and ending with the null pointer to function:

	(int (*)())0

assuming the functions in question do indeed return an int.

Alternatively, you could have the calling function set up an array of
pointers to functions (again terminated with the null pointer), and pass
this array by reference as a pointer to a pointer to a function returning int:

	int (**)()

There is nothing illegal about this.

	extern int one(), two(), three(), four(), five();

	int (*table[])() = { one, two, three, four, five, (int (*)())0 };

To be initialized, the array must be declared either global or static, but
there should be no other problem since the function addresses are known
at compile time.

-- 
					Lloyd Kremer
					Brooks Financial Systems
					...!uunet!xanth!brooks!lloyd
					Have terminal...will hack!



More information about the Comp.lang.c mailing list