6

What is the difference between the two declarations in case of foo's arguments? The syntax in the second one is familiar to me and declares a pointer to function. Are both declarations fully equivalent?

void foo(int(int));
void foo(int(*)(int));
lukeg
  • 4,189
  • 3
  • 19
  • 40
  • [this answer](https://stackoverflow.com/a/3674225/65863) has some useful information about the use of, and lack of, an asterisk in function pointer declarations. – Remy Lebeau May 24 '18 at 00:44
  • Also see: [Is the non-pointer syntax for declaring function pointer parameters not worth mentioning?](http://scottmeyers.blogspot.com/2014/07/is-non-pointer-syntax-for-declaring.html) – Remy Lebeau May 24 '18 at 00:45
  • gcc thinks that they are equal in this context: https://ideone.com/snKfj6 – Slava May 24 '18 at 00:47

1 Answers1

7

They are equivalent as long as int(int) and int(*)(int) are used in function parameter lists. In function parameter list the int(int) is automatically adjusted by the language to mean int(*)(int).

It is the same adjustment mechanism that makes int [] parameter declaration equivalent to int * parameter declaration.

Outside of this specific context int(int) and int(*)(int) mean two different things.

AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765
  • Thanks! Can you elaborate on what are the two different meanings of the delcaration outside of function parameter context? Do you mean that one declares a function type and the other a pointer? I.e. something that has already been pointed out in the comments to my question? – lukeg May 24 '18 at 10:30