Function pointer in argument list

Discussion in 'C Programming' started by hectorchu@gmail.com, Mar 26, 2009.

  1. Guest

    Does anyone know whether the (*) is optional around the function
    pointer name when it is part of an argument list, e.g.

    int f(int fn(int))
    {
    return fn(5);
    }

    ?
     
    , Mar 26, 2009
    #1
    1. Advertising

  2. Guest

    Thank you for all your contributions. The C99 reference seems to
    answer my question conclusively.
     
    , Mar 26, 2009
    #2
    1. Advertising

  3. Richard Heathfield <> writes:
    > said:
    >> Does anyone know whether the (*) is optional around the function
    >> pointer name when it is part of an argument list, e.g.


    You mean parameter list, not argument list. An argument list is the
    list of expressions in a function call.

    >> int f(int fn(int))
    >> {
    >> return fn(5);
    >> }
    >>
    >> ?

    >
    > Believe it or not, it *is* optional. 3.2.2.1 of C89 reads in part:
    > "A function designator is an expression that has function type.
    > Except when it is the operand of the sizeof operator /25/ or the
    > unary & operator, a function designator with type ``function
    > returning type '' is converted to an expression that has type
    > ``pointer to function returning type .''"


    Yes, but that's not the section that says it's optional. The text you
    quoted says that the "*" is optional in a function call, not in a
    function declaration.

    The relevant paragraph is C99 6.7.5.3p8:

    A declaration of a parameter as "function returning _type_" shall
    be adjusted to "pointer to function returning _type_", as in
    6.3.2.1.

    > And I don't mind admitting that my initial reaction was "no". But I
    > was wrong.


    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Mar 26, 2009
    #3
  4. Richard Heathfield <> writes:

    > Keith Thompson said:
    >
    >> Richard Heathfield <> writes:

    > <snip>
    >>>
    >>> Believe it or not, it *is* optional. 3.2.2.1 of C89 reads in
    >>> part: "A function designator is an expression that has function
    >>> type. Except when it is the operand of the sizeof operator /25/
    >>> or the unary & operator, a function designator with type
    >>> ``function returning type '' is converted to an expression that
    >>> has type ``pointer to function returning type .''"

    >>
    >> Yes, but that's not the section that says it's optional. The text
    >> you quoted says that the "*" is optional in a function call, not
    >> in a function declaration.
    >>
    >> The relevant paragraph is C99 6.7.5.3p8:
    >>
    >> A declaration of a parameter as "function returning _type_"
    >> shall be adjusted to "pointer to function returning _type_",
    >> as in 6.3.2.1.

    >
    > Oops, nice catch. Thanks, Keith. The corresponding C89 quote is in
    > 3.7.1, with the same wording, modulo the capital A and the
    > cross-ref.


    If it helps reduce the surprise factor, this is somewhat analogous to
    the treatment of parameters that look as if they have an array type.

    I too, only came across this very recently due to a typo and a
    preference for keeping *s out of typedefs. I'd written:

    typedef int func(int);

    int some_function(func f) /* forgot the * by accident! */
    {...}

    and only found out that I should have been surprised that it worked
    some time later when fixing something else! On the other hand, I
    would not have been surprised that

    typedef int vector[3];

    int other_function(vector v) {...}

    makes 'other_function' of type 'int(int *)' so there is some
    similarity. Of course, the similarity stops when you *do* add the *.
    It has no effect on 'some_function' but changes the type of
    'other_function' to be 'int(int (*)[3])'.

    --
    Ben.
     
    Ben Bacarisse, Mar 26, 2009
    #4
    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. Bernhard

    'dynamic' function pointer argument list

    Bernhard, Jul 16, 2004, in forum: C Programming
    Replies:
    6
    Views:
    340
    Bernhard
    Jul 16, 2004
  2. Ben Kial
    Replies:
    1
    Views:
    659
    Eric Enright
    Nov 15, 2004
  3. Vijai Kalyan
    Replies:
    4
    Views:
    708
    Vijai Kalyan
    Nov 8, 2005
  4. Reckoner
    Replies:
    11
    Views:
    703
    Steven D'Aprano
    Jan 19, 2009
  5. AikidoGuy
    Replies:
    11
    Views:
    560
    Seebs
    Nov 21, 2011
Loading...

Share This Page