Path: utzoo!utgpu!watserv1!watmath!att!rutgers!usc!zaphod.mps.ohio-state.edu!rpi!crdgw1!underdog!volpe From: volpe@underdog.crd.ge.com (Christopher R Volpe) Newsgroups: comp.lang.c Subject: Re: Type of function returning function. Message-ID: <9815@crdgw1.crd.ge.com> Date: 16 Jul 90 17:13:16 GMT References: <1852@ns-mx.uiowa.edu> <1990Jul10.024205.17382@media.uucp> <20299@grebyn.com> Sender: news@crdgw1.crd.ge.com Reply-To: volpe@underdog.crd.ge.com (Christopher R Volpe) Lines: 60 In article <1852@ns-mx.uiowa.edu>, williams@umaxc.weeg.uiowa.edu (Kent Williams) writes: > > typedef SOMETYPE fp; > fp state2(), state3(); > > fp state1() { return state2; } > fp state2() { return state3; } > fp state3() { return (fp)NULL; } > > void statemachine() { > fp current; ^^^^^^^ > current = state1; > while((current = (current)()) != NULL) > ; > } > Minor point, but "current" doesn't hold an fp, it holds the address of a function returning an fp, right? fp (*current)(); Also, you can take advantage of the self referential abilities of structs to do what you want with functions without any typecasts, although it is debatable whether or not the following is any less kludgy (it does compile under gcc, though): typedef struct somestruct { struct somestruct (*field)(); } fp; fp f1(),f2(); fp f1() { fp dummy; dummy.field=f2; return dummy; } fp f2() { fp dummy; dummy.field=f1; return dummy; } void statemachine() { fp (*current)(); current = f1; while((current = (current)().field) != NULL) ; } Chris Volpe G.E. Corporate R&D volpecr@crd.ge.com