A great C example code for Finite State Machine
*From Stackoverflow
http://stackoverflow.com/questions/1371460/state-machines-tutorials
State machines are very simple in C if you use function pointers.
Basically you need 2 arrays – one for state function pointers and one for state transition rules. Every state function returns the code, you lookup state transition table by state and return code to find the next state and then just execute it.
int entry_state(void); int foo_state(void); int bar_state(void); int exit_state(void); /* array and enum below must be in sync! */ int (* state[])(void) = { entry_state, foo_state, bar_state, exit_state}; enum state_codes { entry, foo, bar, end}; enum ret_codes { ok, fail, repeat}; struct transition { enum state_codes src_state; enum ret_codes ret_code; enum state_codes dst_state; }; /* transitions from end state aren't needed */ struct transition state_transitions[] = { {entry, ok, foo}, {entry, fail, end}, {foo, ok, bar}, {foo, fail, end}, {foo, repeat, foo}, {bar, ok, end}, {bar, fail, end}, {bar, repeat, foo}}; #define EXIT_STATE end #define ENTRY_STATE entry int main(int argc, char *argv[]) { enum state_codes cur_state = ENTRY_STATE; enum ret_codes rc; int (* state_fun)(void); for (;;) { state_fun = state[cur_state]; rc = state_fun(); if (EXIT_STATE == cur_state) break; cur_state = lookup_transitions(cur_state, rc); } return EXIT_SUCCESS; }
Reading C type declarations
Original post by Steve Friedl’s http://unixwiz.net/techtips/reading-cdecl.html
A really great tutorial to understand C precedence.
A simple example
We’ll start with a simple example:
long **foo[7];
We’ll approach this systematically, focusing on just one or two small part as we develop the description in English. As we do it, we’ll show the focus of our attention in red, and strike out the parts we’ve finished with.
long **foo [7];
Start with the variable name and end with the basic type:
foo is … long
long ** foo[7];
At this point, the variable name is touching two derived types: “array of 7” and “pointer to”, and the rule is to go right when you can, so in this case we consume the “array of 7”
foo is array of 7 … long
long ** foo[7];
Now we’ve gone as far right as possible, so the innermost part is only touching the “pointer to” – consume it.
foo is array of 7 pointer to … long
long * *foo[7];
The innermost part is now only touching a “pointer to”, so consume it also.
foo is array of 7 pointer to pointer to long
This completes the declaration!
……
http://yannesposito.com/Scratch/en/blog/Learn-Vim-Progressively/
git – the simple guide
git – the simple guide
very neat and brief guide
OpenCV / SDL / Chipmunk – Webcam image processing with physical engine
Amazing!