[comp.lang.c] Array of pointers to functions

dooley@helios.toronto.edu (Kevin Dooley) (06/12/89)

I had a clever idea last night for a completely general routine to
numerically solve a system of N coupled first order differential
equations simultaneously using a Runge-Kutta algorithm.  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.  Why not?  Can anybody help me out with this little
puzzle?
		Many thanks,
			Kevin Dooley

-- 
 Kevin Dooley         UUCP - {uunet,pyramid}!utai!helios.physics!dooley
 Physics Dept.        BITNET - dooley@utorphys
 U. of Toronto        INTERNET - dooley@helios.physics.utoronto.ca

kremer@cs.odu.edu (Lloyd Kremer) (06/13/89)

>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!

woodman@sumax.UUCP (David Woodman) (12/05/89)

Hello again,
  I need an array of pointers to functions but would like some
to return ints, others to return pointers and others nothing.
Is this possible? If so, how?

-- 
------------------------------------------------------------------------
David Woodman          MAIL: woodman%sumax.uucp@beaver.cs.washington.edu
Seattle University     #include <disclaimer.std>