Question

gccseems to be telling me int foo(int (*f)(void)) is the same as int foo(int (f)(void)) f is a pointer to a function taking no argument returning int. Why can we omit the * here.

However, I also tried int (*p)(void) and int (p)(void). The first p is a function pointer, while the second is a function.

So, what's happening? Under what circumstances can the asterisk be omitted? I look it up in K&R and discovered nothing.

Était-ce utile?

La solution

To quote from my copy of Harbison and Steele (5th edition, section 9.3):

In parameter lists, a type "function returning T" is implicitly rewritten to have type "pointer to function returning T".

That means that in the context of a formal parameter list, they are equivalent. In any other context, they are different.

As a matter of style, I would never omit the *.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top