Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.2 9/18/84; site brl-tgr.ARPA Path: utzoo!watmath!clyde!burl!ulysses!allegra!mit-eddie!think!harvard!seismo!brl-tgr!tgr!cottrell@nbs-vms.ARPA From: cottrell@nbs-vms.ARPA (COTTRELL, JAMES) Newsgroups: net.lang.c Subject: I Do Declare! Message-ID: <1113@brl-tgr.ARPA> Date: Thu, 29-Aug-85 17:09:59 EDT Article-I.D.: brl-tgr.1113 Posted: Thu Aug 29 17:09:59 1985 Date-Received: Sat, 31-Aug-85 08:19:16 EDT Sender: news@brl-tgr.ARPA Lines: 41 /* In article <368@persci.UUCP> roman@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@nbs */ ------