I Do Declare!
COTTRELL, JAMES
cottrell at nbs-vms.ARPA
Fri Aug 30 07:09:59 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...
Cheat! Declare your variable as a pointer to funxion returning int
(or char (or void) pointer for sticklers) and then use casts.
Saves a lot of headaches trying to figure out all those types.
With char or void pointers, there ought to be a portable way.
typedef int (*PFI)();
typedef char *(*PFCP)(); /* for portability sticklers */
typedef void *(*PFVP)(); /* if available */
int other();
int init(); /* initial func */
PFI state = init; /* state var */
init(arg)
{
state = other; /* no problem to set directly */
...
return((int)state); /* to return we must cast */
...
state = (PFI)other(arg);/* must cast to assign */
...
(*state)(arg); /* can be called directly */
...
while (state = (PFI)(*state)(arg)); /* until no more funx */
}
Once in a while you get shown the light
In the strangest of places if you look at it right.
jim cottrell at nbs
*/
------
More information about the Comp.lang.c
mailing list