Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Re: syntax for calling functions by pointers

Reply
Thread Tools

Re: syntax for calling functions by pointers

 
 
Nick Keighley
Guest
Posts: n/a
 
      09-05-2003
(Bert Douglas) wrote in message news:<. com>...

> When you have a pointer to a function, like this:
> int (*f)(void);
>
> I have always seen it invoked like this:
> result = (*f)();
>
> But now I see code that does simply this:
> result = f();
>
> The second style seems to be valid in Microsoft and Intel compilers.
> Even when you tell it to compile as standard C.
>
> I looked in the C language reference manual, and I don't see this
> syntax.
>
> Is this a new extension from C++, or C99 ? Or was it always like
> this?


it was always thus. Well from ANSI onwards (1989). I'm not sure about
K&R. some people think the (*f) form is clearer because it makes it
obvious you are calling thru a pointer.

In the ANSI standard it's section 3.3.3.2, but I don't find it an
obvious read. The Rationale is quite clear.

"Pointers to functions may be used either as (*pf)() or pf().
The latter construct, not sanctioned by the Base Document
[ie. K&R], [...] is unambiguous and invalidates no old code"

"all the following expressions are valid function calls
(&f)(); f(); (*f)(); (**f)(); (***f)();
pf(); (*pf)(); (**pf)(); (***pf)();"

ain't C fun?



--
Nick Keighley

"As I recall, OSI dealt with TCP/IP by just admitting it into the spec as a
variation of existing levels. This is akin to dealing with an Alien face
hugger by allowing it to implant its embryo in your body."
 
Reply With Quote
 
 
 
 
Tom Zych
Guest
Posts: n/a
 
      09-05-2003
Nick Keighley wrote:

> In the ANSI standard it's section 3.3.3.2, but I don't find it an
> obvious read. The Rationale is quite clear.


> "Pointers to functions may be used either as (*pf)() or pf().
> The latter construct, not sanctioned by the Base Document
> [ie. K&R], [...] is unambiguous and invalidates no old code"


I understand this to mean that, even though K&R said you shouldn't
use pf() (or, perhaps, didn't say you could), it has actually
always been kosher to do so. Correct?

If so, shouldn't this verbiage in FAQ 4.12 be changed?

"Originally, a pointer to a function had to be ``turned into'' a
``real'' function..."

Along the lines of
s/Originally,/Originally, it was believed that/

Perhaps I'm being too pedantic...

--
Tom Zych
This email address will expire at some point to thwart spammers.
Permanent address: echo '' | rot13
 
Reply With Quote
 
 
 
 
Steve Summit
Guest
Posts: n/a
 
      09-08-2003
[I already mailed this to Tom, but for the record...]

Tom Zych wrote:
>> "Pointers to functions may be used either as (*pf)() or pf().
>> The latter construct, not sanctioned by the Base Document
>> [ie. K&R], [...] is unambiguous and invalidates no old code"

>
> I understand this to mean that, even though K&R said you shouldn't
> use pf() (or, perhaps, didn't say you could), it has actually
> always been kosher to do so. Correct?


No, not quite. Ritchie's original C compiler required the
explicit (*). "Invalidates no old code" means that there was
no old code using the new, abbreviated syntax, because it was
formerly disallowed. (If memory serves, the error message was
"call of non-function".)

> If so, shouldn't this verbiage in FAQ 4.12 be changed?
>
> "Originally, a pointer to a function had to be ``turned into'' a
> ``real'' function..."
>
> Along the lines of
> s/Originally,/Originally, it was believed that/
>
> Perhaps I'm being too pedantic...


Nice wording, but unnecessary here. (One issue it would apply
to, though, would be parentheses around the expression in a
return statement...)

Steve Summit

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
pointers, pointers, pointers... cerr C Programming 12 04-07-2011 11:17 PM
Pointers to char pointers in functions newbie C Programming 9 09-24-2006 10:31 AM
Questions about pointers to objects and pointers to functions Marc Thrun C Programming 15 10-04-2005 05:47 PM
Function pointers, variable argument functions calling other variable-argument functions (sort of) S?ren Gammelmark C Programming 1 01-07-2005 09:41 PM
Re: syntax for calling functions by pointers Stephan Wilms C Programming 2 09-06-2003 05:07 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57