Path: utzoo!attcan!utgpu!jarvis.csri.toronto.edu!rutgers!usc!gem.mps.ohio-state.edu!uakari.primate.wisc.edu!xanth!mcnc!rti!xyzzy!kan From: kan@dg-rtp.dg.com (Victor Kan) Newsgroups: comp.lang.c Subject: Re: pointers to functions, HELP Summary: real ugly, but it works Keywords: pointers to functions Message-ID: <2119@xyzzy.UUCP> Date: 25 Oct 89 20:26:06 GMT References: <20057@ut-emx.UUCP> Sender: usenet@xyzzy.UUCP Reply-To: kan@mutley.dg.com () Organization: Data General Corporation, Research Triangle Park, NC Lines: 74 In article <20057@ut-emx.UUCP> mike@ut-emx.UUCP (Mike O'Donnell) writes: > >Although pointers to functions are not totally new to me, I have >begun some experimentation with them and have a few questions. > >1. If possible, how does one declare a function that returns > a pointer to a function? And if possible, how does one use it? > >2. How do you declare an array of pointers to functions? > >Any help and suggestions will be greatly appreciated. > >Thanks, Mike O'Donnell > This code works in GNU cc 1.35 running on an DG AViiON (m88k) running Unix. I wouldn't be surprised if Microsoft C 5.0 (the piece of #*%&@#!) barfs on it though. -- cut here -- #include typedef int (*pointer_to_function_returning_int)(); typedef pointer_to_function_returning_int (*pointer_to_function_returning_pointer_to_function_returning_int)(); /* to answer question #2: */ pointer_to_function_returning_int array_of_pointers_to_functions[100]; int returns_int(); pointer_to_function_returning_int returns_pointer_to_function_returning_int(); int returns_int() { printf ("returns_int() works!\n"); return (1); } /* I haven't tried this without the typedefs, but I'm sure * you don't want to try either!!! */ pointer_to_function_returning_int returns_pointer_to_function_returning_int() { printf ("returns_pointer_to_function_returning_int() works!\n"); return (returns_int); } main() { pointer_to_function_returning_int p1; pointer_to_function_returning_pointer_to_function_returning_int p2; p1 = returns_int; p2 = returns_pointer_to_function_returning_int; (*p1)(); /* not too bad */ (*(*p2)())(); /* really disgusting, but beautiful in a weird sort of way */ } -- cut here -- The output looks like this: returns_int() works! returns_pointer_to_function_returning_int() works! returns_int() works! | Victor Kan | I speak only for myself. | *** | Data General Corporation | Edito cum Emacs, ergo sum. | **** | 62 T.W. Alexander Drive | Columbia Lions Win, 9 October 1988 for | **** %%%% | RTP, NC 27709 | a record of 1-44. Way to go, Lions! | *** %%%