Hi,
I would like to use function pointers to code a state machine.
That is if I have the following events possible in 2 states S1, S2
S1: E1, E2
S2: E3, E4
and I have processing functions for all possible events for all states, and corresponding function pointers
fpS1E1, fpS1E2, fpS2E3, fpS2E4, corresponding to the processing of a particular event in a particular state.
Now if I define a function pointer two dimensional array like
the state machine will function right? Or is there any problem in my logic/code?Code:void *fpArray[][]() = { {fpS1E1,fpS1E2}, {fpS2E3,fpS2E4}}; and if #define S1 0 #define S2 1 #Define E1 0 #define E2 1, state = S1; while (1) { eventid = receive_event(); fpArray[state][eventid]; } where typically inside function fpS1E1() { ... state = S2 (any new state) }
I have omitted function pointer initialisations( fpS1E1 = s1e1function and the like)
Meena



LinkBack URL
About LinkBacks


