yasar
yasar

Reputation: 13738

arrays, pointers and pointers to functions in finite-state-machine

I was reading this: http://johnsantic.com/comp/state.html and having hard time comprehending this part:

void (*const state_table [MAX_STATES][MAX_EVENTS]) (void) = {

    { action_s1_e1, action_s1_e2 }, /* procedures for state 1 */
    { action_s2_e1, action_s2_e2 }, /* procedures for state 2 */
    { action_s3_e1, action_s3_e2 }  /* procedures for state 3 */
};

Can someone please explain what is going on here?

Upvotes: 1

Views: 1051

Answers (2)

Lundin
Lundin

Reputation: 213583

If you un-ofuscate the code with a typedef, it actually becomes readable:

typedef void (*const fptr_state) (void);

fptr_state  state_table [MAX_STATES][MAX_EVENTS] = 
{
    { action_s1_e1, action_s1_e2 }, /* procedures for state 1 */
    { action_s2_e1, action_s2_e2 }, /* procedures for state 2 */
    { action_s3_e1, action_s3_e2 }  /* procedures for state 3 */
};

Upvotes: 1

asaelr
asaelr

Reputation: 5456

It defines 2D array of pointers functions (return void).

state_table [MAX_STATES][MAX_EVENTS] means that state_table is 2D array, and the void (*expression)(void) means that expression is pointer to a function, that takes no arguments (this is the mean of (void) as argument list), and returns void.

The other lines just initializes the array.

Upvotes: 4

Related Questions