Re: quik popularized by ANSI C

From: Arthur J. O'Dwyer (ajo_at_nospam.andrew.cmu.edu)
Date: 06/14/04


Date: Mon, 14 Jun 2004 09:37:59 -0400 (EDT)


On Sun, 13 Jun 2004, Jack Klein wrote:
>
> On Sun, 13 Jun 2004 22:42:13 +0100, "Malcolm" wrote:
> > "Jack Klein" <jackklein@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



Relevant Pages

  • Re: Help structuring my program (arrays of function pointers/ passing variables to functions)
    ... Don't force different function pointer types to one type. ... (all these functions in some array of function ... Array of function prototypes? ... function_circle(int x, int y, char color, char text) based on ...
    (comp.lang.c)
  • Re: qsort (lib function)
    ... > I am having to pass an array of structures to qsort, ... pointers to functions taking two const void * parameters and returning int. ... The function pointer type will, presumably, be used to define ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Functions
    ... I dont know it is possible or not ... now main wants to call the function whose name present inside string. ... //read the function name in array ... Now using this function pointer you can call the function. ...
    (comp.lang.c)
  • Re: When to use complex declarations like the following.....?
    ... > function pointer to return to callback on receiving an arbitrary ... function pointers are always needed when you have callback ... Then a simple way to realize that would be to have an array ... the use of functions that get loaded by some plug-in mechanism ...
    (comp.lang.c)
  • Re: list of pointers to function
    ... So my list (or array?) should be able to keep pointers to every kind of function. ... A function pointer of one type can be freely converted to a function pointer of another type as long as they are converted back to the correct type before calling the function. ... typedef void; ... Linux Registerd User #338167 ...
    (comp.lang.c)