function returning pointer to itself

Jeff Siegal jbs at fenchurch.MIT.EDU
Wed Jul 13 18:26:33 AEST 1988


In article <5485 at batcomputer.tn.cornell.edu> olson at tcgould.tn.cornell.edu (olson) writes:
>Does the new C standard have a natural way to declare a
>function that returns a pointer to itself
>????

I'm not sure about a natural way, but the following should suffice:

typedef void *(*NextStateFun)();

static void *state1();
static void *state2();
static void *state3();

void *state1() { return (void *)state2; }
void *state2() { return (void *)state3; }
void *state3() { return NULL; }
void * const initial_state = (void *)state1;
.
.
.
    void *state = initial_state;
  
    while (state) { state = (*(NextStateFun)state)(); }



More information about the Comp.std.c mailing list