What is the difference between int* ptr(int, int)
and int (*ptr)(int, int)
??
Please explain me it details that's what's happening here.

- 14,524
- 7
- 33
- 80

- 59
- 1
- 1
- 11
-
1The first is a function named `ptr` that returns a pointer to an `int`, and `ptr` is not a variable. In the second `ptr` is a variable which points to a function that returns an `int`, in other words is a variable that contains the address of a function of that type. – Weather Vane Mar 28 '20 at 11:53
1 Answers
1.
int* ptr(int, int)
ptr
is a function which takes two int
arguments and returns a pointer to int
.
2.
int (*ptr)(int, int)
ptr
is a pointer to a function which takes two int
arguments and returns an int
.
The latter makes sense if you f.e. have several optional functions available but want to choose a specific one by a particular matching condition. Then you build up an array of function pointers and pick the desired function pointer. But notice that the pointed function here is a a little bit different then the one at the first example as it is returning an int
, not a pointer to an int
.
If the function pointed to should be equivalent to the first form with returning a pointer to int
you need to do:
int* (*ptr)(int, int)
If you only have one function to go with you don´t need to addressing it via a pointer and rather use the first form.
If you want to learn more about function pointers, here is a link to a dedicated SO question:

- 1
- 1

- 14,524
- 7
- 33
- 80
-
-
@MolybdenumOxide I think the answers under this question explain it at its best: https://stackoverflow.com/questions/840501/how-do-function-pointers-in-c-work – RobertS supports Monica Cellio Mar 28 '20 at 12:09
-
-
1@MolybdenumOxide That is a huge topic on its own. I recommend you to read a good C starting book first. F.e. this one: https://kremlin.cc/k&r.pdf – RobertS supports Monica Cellio Mar 28 '20 at 14:56
-
@MolybdenumOxide [The Definitive C Book Guide and List](https://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list) - Here you can find a great source of collected knowledge. – RobertS supports Monica Cellio Mar 28 '20 at 14:57