Re: syntax for calling functions by pointers

Discussion in 'C Programming' started by Nick Keighley, Sep 5, 2003.

  1. (Bert Douglas) wrote in message news:<>...

    > 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."
     
    Nick Keighley, Sep 5, 2003
    #1
    1. Advertising

  2. Nick Keighley

    Tom Zych Guest

    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
     
    Tom Zych, Sep 5, 2003
    #2
    1. Advertising

  3. Nick Keighley

    Steve Summit Guest

    [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
     
    Steve Summit, Sep 8, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Stephan Wilms

    Re: syntax for calling functions by pointers

    Stephan Wilms, Sep 5, 2003, in forum: C Programming
    Replies:
    2
    Views:
    321
    Richard Heathfield
    Sep 6, 2003
  2. S?ren Gammelmark
    Replies:
    1
    Views:
    1,899
    Eric Sosman
    Jan 7, 2005
  3. Marc Thrun
    Replies:
    15
    Views:
    863
    Tim Rentsch
    Oct 4, 2005
  4. newbie

    Pointers to char pointers in functions

    newbie, Sep 18, 2006, in forum: C Programming
    Replies:
    9
    Views:
    310
    August Karlstrom
    Sep 24, 2006
  5. cerr

    pointers, pointers, pointers...

    cerr, Apr 7, 2011, in forum: C Programming
    Replies:
    12
    Views:
    683
Loading...

Share This Page