C
celsius
Hi all,
please forgive me if this already posted many times.
i was reading peter van der linden's book expert C programming.
on page number 188,he is discussing about implementing finite state
machine in C.
he explains as follows :-
there are several ways to implement finite state
machines in C,but the most common one is is array of pointers to
functions.an array of pointers to functions can be declared like this:
void (*state[MAX_STATES])(); if you function names,you can initialize
the array like so:
extern int a(),b(),c(),d();
int (*state[])() = {a,b,c,d};
a function can be called through a pointer to array like this
(*state)();.pointers to function can be funny.notice,too, how
the pointer can be dropped, so our call can be made equally be made as
state(); or even (******state)();
this is an unfortunate quirk popularized by ANSI C:calls to a function
and calls to a function through a pointer(or any level of pointer
indirection) can use same syntax
there is a corresponding quirk that applying to arrays.it further
undermines the flawed "declaration looks like use" philosophy.
now my question is
1)why this unfortunate quirk was made possible in ANSI C ?
2) there is a corresponding quirk that applying to arrays,i would
like to know what this quirk is ?
please forgive me if this already posted many times.
i was reading peter van der linden's book expert C programming.
on page number 188,he is discussing about implementing finite state
machine in C.
he explains as follows :-
there are several ways to implement finite state
machines in C,but the most common one is is array of pointers to
functions.an array of pointers to functions can be declared like this:
void (*state[MAX_STATES])(); if you function names,you can initialize
the array like so:
extern int a(),b(),c(),d();
int (*state[])() = {a,b,c,d};
a function can be called through a pointer to array like this
(*state)();.pointers to function can be funny.notice,too, how
the pointer can be dropped, so our call can be made equally be made as
state(); or even (******state)();
this is an unfortunate quirk popularized by ANSI C:calls to a function
and calls to a function through a pointer(or any level of pointer
indirection) can use same syntax
there is a corresponding quirk that applying to arrays.it further
undermines the flawed "declaration looks like use" philosophy.
now my question is
1)why this unfortunate quirk was made possible in ANSI C ?
2) there is a corresponding quirk that applying to arrays,i would
like to know what this quirk is ?