Function pointer in argument list

H

hectorchu

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);
}

?
 
H

hectorchu

Thank you for all your contributions. The C99 reference seems to
answer my question conclusively.
 
K

Keith Thompson

Richard Heathfield said:
(e-mail address removed) said:

You mean parameter list, not argument list. An argument list is the
list of expressions in a function call.
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.
 
B

Ben Bacarisse

Richard Heathfield said:
Keith Thompson said:


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])'.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top