Re: Cannot understand the following codes
- From: "Arthur J. O'Dwyer" <ajonospam@xxxxxxxxxxxxxx>
- Date: Wed, 29 Mar 2006 18:30:31 -0500 (EST)
On Wed, 29 Mar 2006, Ben C wrote:
On 2006-03-29, Joe Wright <joewwright@xxxxxxxxxxx> wrote:Ben C wrote:[..]
I had assumed void * was good for anything. But it's only good for data,
not functions.
I suppose on some machines functions might have a completely different
address space, be a completely different size etc., and the standard is
allowing for that...
Pedantically void pointers and function pointers are not compatible.
But is a less pedantic world it might work.. if you change your line
int x = (*r)(3);
to
int x = (r)(3); or even
int x = r(3);
Isn't this a different issue? I don't know what the standard says about
r(3), although it usually works.
It is a different issue altogether. Changing (*r) to (r) won't magically make the difference (in C) between data and code pointers go away.
The difference between (*r) and (r) is that (*r) is the thing pointed to by 'r', which is of function type, which, in this context, immediately
decays back to a function pointer. (r) is just that same function pointer, without the dereference and decay. In other words, (*r) decays to (r).
Therefore, both expressions have the /exact same/ behavior.
I think more old code used the (*f)(x) style, and newer code, maybe influenced by C++ and Lispy languages, uses plain old f(x) more often.
I tend to use (*cmp)(x,y) for callback functions in things like 'qsort', but if I were writing something like
int (*printme)() = (widget == on)? magic: more_magic;
I'd use
printme(x);
instead.
HTH,
-Arthur
.
- Follow-Ups:
- Re: Cannot understand the following codes
- From: Ben C
- Re: Cannot understand the following codes
- References:
- Cannot understand the following codes
- From: gpsabove
- Re: Cannot understand the following codes
- From: Alf P. Steinbach
- Re: Cannot understand the following codes
- From: Ben C
- Re: Cannot understand the following codes
- From: Alf P. Steinbach
- Re: Cannot understand the following codes
- From: Ben C
- Re: Cannot understand the following codes
- From: Joe Wright
- Re: Cannot understand the following codes
- From: Ben C
- Cannot understand the following codes
- Prev by Date: Re: returning lvalue in C vs C++
- Next by Date: Re: returning lvalue in C vs C++
- Previous by thread: Re: Cannot understand the following codes
- Next by thread: Re: Cannot understand the following codes
- Index(es):
Relevant Pages
|