Hi,

I'm writing a little program targetting a microcontroller. It's somewhat a state machine, where the program is always looping throw a defined state until it founds that some condition changed and hence must go to a new state.
These states are implemented as individual functions:

Code:
char stateA() {
  for(;;) {
    if(condition1) {
      //do something
      return 'B';  //go to state B
    }
    else if(condition2) {
      //do something else
      return 'D';  //go to state B
    }
  }
}
The main function initially sets the state and then enters an infinete loop switching between states:

Code:
main() {
  char state = 'A';
  for(;;) {
    switch(state) {
      case 'A':
        state = stateA();
        break;
      case 'B':
        state = stateB();
        break;
      case 'C':
        state = stateB();
        break;
      case 'D':
        state = stateD();
        break;
    }
  }
}
This is working find, but it's not very comfortable to add new states because I need to touch a lot of code (the program is big, I have enums, constants, #defines, etc)

So I think a nicer approach would be to make the states functions return a pointer to another state function instead of a char. The problem is I don't know how to declare such a pointer (I don't know if it is even possible) since the type of the function pointed to is the same as the type being defined.

Can anyone tell me how can I do this, it is at all possible ?

Thanks a lot