Solution of array of pointers to functions problem

Steve Lamont spl at mcnc.org
Wed Jun 14 22:37:23 AEST 1989


In article <823 at helios.toronto.edu> dooley at helios.physics.utoronto.ca (Kevin Dooley) writes:
<In a very recent posting I asked why it was illegal to construct
<an array of pointers to functions.  The answer is, of course, that
<it is completely legal, but with a twist.  The answer was graciously
<suplied by Steve Lamont (thanks again).  Here is the expurgated version
<of what he said:
<
<>  ...Assuming that you want to do something like
<>
<>	double	foo();
<>	double	bar();
<>	double	baz();
<>	double	woof();
<>	double	arf();
<>
<>	typedef double	(*function)();
<>
<>	function	functionList[] = { foo, bar, baz, woof, arf };
<>
<>you can then invoke your favorite function in the following manner:
<>
<>	someReturnValue = (*functionList[index])( your favorite args );
<
<Steve points out that the typedef is critical, ie
<
<	double	(*functionList[])() = { ... };
<
<*DOES**NOT**WORK*.  This is the peculiarity that I was missing.  Now
<everything works beautifully.  So the big question at this point is
<why is the typedef necessary?  I thought that typedef was *NEVER* 
<required.  Anybody know the answer?
<

Actually it *does* work (blush!) as a previous poster showed.  When I
cobbled up my example (yes, I *do* try to test my code before I reply to
someone's question), I guess that I managed to bungle something.  The
codelet (code fragment?)

	double	foo();
	double	bar();
	double	baz();
	double	woof();
	double	arf();

	double	(*functionList[])() = { foo, bar, baz, woof, arf };

indeed *is* valid.  I have not the slightest idea how I managed to cause
it *not* to work in the first place, but I can't seem to make it break
now. :-(

I just tested it again on two compilers, our BSD VAX and my perverted
SysV IRIS 4D and they're both quite happy with it.


-- 
							spl
Steve Lamont, sciViGuy			EMail:	spl at ncsc.org
North Carolina Supercomputing Center	Phone: (919) 248-1120
Box 12732/RTP, NC 27709



More information about the Comp.lang.c mailing list