Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!asuvax!ncar!elroy.jpl.nasa.gov!swrinde!zaphod.mps.ohio-state.edu!wuarchive!udel!haven!adm!smoke!gwyn From: gwyn@smoke.brl.mil (Doug Gwyn) Newsgroups: comp.lang.c Subject: Re: Recursive function pointer type. How? Message-ID: <15461@smoke.brl.mil> Date: 13 Mar 91 16:47:12 GMT References: <5144@lure.latrobe.edu.au> Organization: U.S. Army Ballistic Research Laboratory, APG, MD. Lines: 34 In article <5144@lure.latrobe.edu.au> ECSGRT@lure.latrobe.edu.au (GEOFFREY TOBIN, ELECTRONIC ENGINEERING) writes: >One way *around* the problem is to use "typedef void (* state) ();", >and cast the function return type, but I'd like to know whether >anyone can construct a 'state' type as in the "machine" function. A type along those general lines is the best that one can do, as C does not fully support recursive types. It does support some use of incomplete types in recursive declarations where a structure (for example) contains a pointer to its own type, but these escapes do not help for declaring such recursive functions as in this example. (The basic problem is that there is no way to declare an incomplete type for use in the declaration that completes the type.) What your colleague is seeking is eminently reasonable, but in C a small amount of kludgery is required in implementing the idea. Here is an example of how one can obtain the desired behavior: typedef void (*state)(void); #define end_state ((state)0) /* "end state" indicator */ ... extern state st_001(void); /* typical state implementation */ state start(void) { /* "start state" implementation */ ... return (state)st_001; } ... int main(void) { register state s; for (s = (state)start; s != end_state; ) s = (*(state (*)())s)(); /* first "*" is optional */ return 0; }