-2

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.

MolyOxide
  • 59
  • 1
  • 1
  • 11
  • 1
    The 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 Answers1

4

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:

How do function pointers in C work?

Community
  • 1
  • 1