Path: utzoo!utgpu!jarvis.csri.toronto.edu!me!radio.astro!helios!dooley From: dooley@helios.toronto.edu (Kevin Dooley) Newsgroups: comp.lang.c Subject: Solution of array of pointers to functions problem Message-ID: <823@helios.toronto.edu> Date: 13 Jun 89 18:33:31 GMT Reply-To: dooley@helios.physics.utoronto.ca (Kevin Dooley) Distribution: na Organization: University of Toronto Physics/Astronomy/CITA Computing Consortium Lines: 43 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@utorphys U. of Toronto INTERNET - dooley@helios.physics.utoronto.ca