Solution of array of pointers to functions problem
Kevin Dooley
dooley at helios.toronto.edu
Wed Jun 14 04:33:31 AEST 1989
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?
PS. I'd like to thank all of the people who supplied correct responses.
I mention Steve's only because it was so concise, correct and ... well,
first. I do not thank any of the people who assumed that I was a complete
idiot (no names, you know who you are). As you see, there was something
nontrivial involved and the subject is not covered in either K&R.
Kevin Dooley
--
Kevin Dooley UUCP - {uunet,pyramid}!utai!helios.physics!dooley
Physics Dept. BITNET - dooley at utorphys
U. of Toronto INTERNET - dooley at helios.physics.utoronto.ca
More information about the Comp.lang.c
mailing list