How do I declare... (recursive typedefs)

Doug Moen kdmoen at watcgl.UUCP
Sun Aug 25 05:42:50 AEST 1985


In article <368 at persci.UUCP> roman at persci.UUCP writes:
>Either I'm missing the perfectly obvious or I've found something
>that's impossible to declare in C, even though it makes sense.  I'm
>trying to implement a simple finite state machine with states
>represented by C functions.  Each state function would accept some
>input value as an argument and return a pointer to the function
>implementing the next state, something like this:
>
>STATE_FUNC (*state)(),   /* the current state         */
>           *state_1(),   /* a function for each state */
>            ... ,
>           *state_n();
>
>   for (;;) {
>      state = (*state)(get_input());
>   }
>
>But how is STATE_FUNC typedef'ed?  It's a pointer to a function which
>returns a pointer to a function which returns a pointer...  Any
>suggestions?

You aren't missing the perfectly obvious.
Yes, it is impossible to declare in C, even though it makes sense.

What you want is a recursive typedef:
	typedef STATE_FUNC (*STATE_FUNC)();
except that this isn't allowed.

What you will have to do instead is a kludge:
	typedef char* (*STATE_FUNC)();

	for (;;) {
		/* Note the cast: */
		state = (STATE_FUNC) (*state)(get_input());
	}
-- 
Doug Moen (watmath!watcgl!kdmoen)
University of Waterloo Computer Graphics Lab



More information about the Comp.lang.c mailing list