473,396 Members | 2,038 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

quik popularized by ANSI C

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[i])();.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[i](); or even (******state[i])();

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 ?
Nov 14 '05 #1
5 1389
On 13 Jun 2004 09:21:49 -0700, at*******@yahoo.co.uk (celsius) wrote
in comp.lang.c:
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[i])();.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[i](); or even (******state[i])();

this is an unfortunate quirk popularized by ANSI C:calls to a function
What is unfortunate about it? Who says it is unfortunate? What are
their qualifications for making that statement? And why are you
talking about a "quirk", when your subject line mentions a "quik"?
For that matter, who says it is a "quirk" or "quik"? This is the way
the language is defined to work.

Does the use of the '+' character as an addition operator qualify as a
"quirk"? Why or why not? Because you, or some unnamed person you do
not cite, does or does not like it?
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
Who says there is a corresponding "quirk" (or "quik") that applies to
arrays.
undermines the flawed "declaration looks like use" philosophy.
Who says that this feature of the languages is either a "philosophy",
or "flawed"? Since neither term appears at all in the ISO/IEC
standard defining the language, they, like "quirk", have no meaning on
this forum. They are apparently matters of somebody's opinion, yet
you state them as fact.
now my question is

1)why this unfortunate quirk was made possible in ANSI C ?
Again, what "unfortunate quirk"? Since the standard does not define
it as such, where does this statement come from? What are the
credentials of the author of these terms, that we should take it
seriously?

More objectively, you have provided absolutely no foundation at all
for the adjective "unfortunate". What is "unfortunate" about this
defined feature of the language? In what way does it limit the use of
C programs or language features that would otherwise not be limited?

Can you cite any instance at all where this language syntax prevents
some program construct that would otherwise be available?

If you don't like the language syntax, feel free to join your national
standards body and volunteer for their C standards committee working
group. Then you may work to have it changed.
2) there is a corresponding quirk that applying to arrays,i would
like to know what this quirk is ?


Again, what "quirk"? You have posted a lot of rubbish loaded with
imprecise opinionated words like "quirk", "flawed", "unfortunate", and
"philosophy", none of which have any defined meaning at all in terms
of C.

I think your particular post is a "quirk", "flawed" by your
"unfortunate" "philosophy".

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #2

"Jack Klein" <ja*******@spamcop.net> wrote in message

What is unfortunate about it? Who says it is unfortunate? What > are their qualifications for making that statement? And why are you talking about a "quirk", when your subject line mentions a
"quik"?

It is a bit silly that

int (*fptr) (int);

fptr = afunction;

x = (*fptr) (1) ;
y = fptr(1);

as exactly the same effect. Of course a function is not a variable, so
dereferencing a function pointer is not exactly meaningful.
Nov 14 '05 #3
On Sun, 13 Jun 2004 22:42:13 +0100, "Malcolm"
<ma*****@55bank.freeserve.co.uk> wrote in comp.lang.c:

"Jack Klein" <ja*******@spamcop.net> wrote in message

What is unfortunate about it? Who says it is unfortunate? What > are

their qualifications for making that statement? And why are
you talking about a "quirk", when your subject line mentions a
"quik"?

It is a bit silly that

int (*fptr) (int);

fptr = afunction;

x = (*fptr) (1) ;
y = fptr(1);

as exactly the same effect. Of course a function is not a variable, so
dereferencing a function pointer is not exactly meaningful.


Why is that any sillier than:

int some_func(const char*);

char ch [] = "Hello";

....when now:

some_func(ch);

....and:

some_func(&ch[0]);

....have exactly the same effect?

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #4

"Jack Klein" <ja*******@spamcop.net> wrote in message
news:he********************************@4ax.com...
On 13 Jun 2004 09:21:49 -0700, at*******@yahoo.co.uk (celsius) wrote
in comp.lang.c:

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[i])();.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[i](); or even (******state[i])();

this is an unfortunate quirk popularized by ANSI C:calls to a function
What is unfortunate about it? Who says it is unfortunate? What are
their qualifications for making that statement? And why are you
talking about a "quirk", when your subject line mentions a "quik"?
For that matter, who says it is a "quirk" or "quik"? This is the way
the language is defined to work.

Does the use of the '+' character as an addition operator qualify as a
"quirk"? Why or why not? Because you, or some unnamed person you do
not cite, does or does not like it?


In what ways does "peter van der linden's book expert C programming.
on page number 188" fail as a citation, other than a slight shortage
of punctuation?
Again, what "quirk"? You have posted a lot of rubbish loaded with
imprecise opinionated words like "quirk", "flawed", "unfortunate", and
"philosophy", none of which have any defined meaning at all in terms
of C.

I think your particular post is a "quirk", "flawed" by your
"unfortunate" "philosophy".


Please read more carefully before going over the top. The poster quoted
van der Linden's book, and effectively asked for a discussion of the
opinions that van der Linden expressed. It is unreasonable to lay into
the poster in this way, when he simply appears to be trying to understand
van der Linden's opinions and reasoning.
Nov 14 '05 #5

On Sun, 13 Jun 2004, Jack Klein wrote:

On Sun, 13 Jun 2004 22:42:13 +0100, "Malcolm" wrote:
"Jack Klein" <ja*******@spamcop.net> wrote in message

What is unfortunate about it?
It is a bit silly that

int (*fptr) (int);

fptr = afunction;

x = (*fptr) (1) ;
y = fptr(1);

as exactly the same effect. Of course a function is not a variable, so
dereferencing a function pointer is not exactly meaningful.


I think it's a quirk, but it has a very good purpose. I generally
try to keep "redirectable function pointer" and "immutable function" as
two separate concepts where useful; thus, I follow Declaration-Mimics-Use
with pointers

int (*fptr)(int);
x = (*fptr)(42);

and functions

int func(int);
y = func(42);

unless I have a darn good reason not to.

Why is that any sillier than:

int some_func(const char*);

char ch [] = "Hello";
some_func(ch);
some_func(&ch[0]);
...have exactly the same effect?


Count the levels of indirection.

ch --- 0 levels
&ch[0] --- 0 levels
fptr --- 0 levels
*fptr --- 1 level

The former pair (Jack's example) have the same effect because they
designate the same object with the same number of levels of indirection.
The latter pair (Malcolm's example) have the same effect *despite*
different levels of indirection. In fact, as van der Linden points
out,

*****fptr --- 5 levels

has --- almost --- exactly the same value and semantics as 'fptr'
(0 levels).

Now, why is this? Simple: like array names, function names "decay"
into function pointers in value contexts. Thus, while 'func' is a
function, '*func' is "the-function-pointed-to-by-the-decayed-function-
pointer-'func'." That is, it's exactly the same function. And then
since '*func' is a function, obviously we can recurse: '**func' is
the function pointed to by the pointer we get by decaying '*func';
'***func' and '****func' and '*********func' are exactly the same
function.

The "--- almost ---" above is a nod to the fact that when 'fptr'
is a function pointer, then 'sizeof fptr' yields the size of the
function pointer type; but 'sizeof *fptr' (and 'sizeof '*****fptr')
try to evaluate the size of an actual function type, which is not
allowed by the rules of the C language. Thus the expressions 'fptr'
and '*fptr' behave differently in the context of 'sizeof' (and also
in the context of '&', where '&*fptr' is 'fptr' itself, but '&fptr'
is the address of the pointer object 'fptr').

As array names do not decay to array pointers, but rather to pointers
to their first elements, there is no "corresponding quirk" with respect
to arrays. I don't know what van der Linden was talking about, there,
but perhaps you ought to read the next few pages; I bet he explains his
statement. I'll check next time I see the book.

HTH,
-Arthur
Nov 14 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Eric Myers | last post by:
Hello folks: (This message is also posted on the help forum at the pexpect sourceforge page, but all indentation in the code got stripped away when I submitted the post.) For some time I've...
100
by: Roose | last post by:
Just to make a tangential point here, in case anyone new to C doesn't understand what all these flame wars are about. Shorthand title: "My boss would fire me if I wrote 100% ANSI C code" We...
4
by: Luke Wu | last post by:
I am just wondering what the following terms usually mean: 1) "Standard C" 2) "K&R C" 3) "ANSI C" I am pretty sure "ANSI C" usually refers to the C89 standard, but what
83
by: sunny | last post by:
Hi All What is C99 Standard is all about. is it portable, i mean i saw -std=C99 option in GCC but there is no such thing in VC++.? which one is better ANSI C / C99? can i know the major...
7
by: Paul Connolly | last post by:
char *s = "Hello"; s = 'J'; puts(s); might print "Jello" in a pre-ANSI compiler - is the behaviour of this program undefined in any pre-ANSI compiler - or would it always have printed "Jello"...
127
by: bz800k | last post by:
Hi Does this code satisfy ANSI C syntax ? void function(void) { int a = 2; a = ({int c; c = a + 2;}); /* <<-- here !! */ printf("a=%d\n", a);
0
NeoPa
by: NeoPa | last post by:
ANSI-89 v ANSI-92 Before we get into all the various types of pattern matching that can be used, there are two ANSI standards used for the main types of wildcard matching (matching zero or more...
41
by: jaysome | last post by:
It's been almost eight years since ISO/IEC approved ISO/IEC 9899:1999. Does anyone know if ANSI has approved it? A Google search shows arguably confusing answers as to whether ANSI has...
6
by: Peng Yu | last post by:
Hi, ANSI and GNU C are different in some delicate aspects (I'm not sure about C++). For example, M_PI is not in ANSI C but in GNU C. Of course, to make my program most portable, I should go...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.