• C

What is the difference between function to a pointer and function pointer

int *function_pointer() - This is a function pointer.

int (*f)();  - This is pointer to a function.

What is the difference. and when should we use it.
Who is Participating?
HooKooDooKuConnect With a Mentor Commented:
int *function_pointer()
This is a function that goes by the name 'function_pointer'.  The function return value is a pointer that points to an integer.  

In C/C++ programming, functions that return a pointer are quite common.  As an example, the function malloc() allocates a block of memory and returns a pointer to that block of memory.

int (*f)()
This is a pointer to a function.  The name of the pointer is 'f'.  But the function it points to could be any function that takes no parameters and returns an int.  

In C/C++ programming, function pointers are relatively rare.  The only place they are common is in API functions that take a call-back function (i.e. a function that's going to do some body of work, and a part of that work is going to include calling a function that you specify).  

Function pointers have other powerful uses besides just being passed as call-back functions.  They can also be used as a part of lookup tables.  This is where you have a large number of functions that you access them via an array of function pointers.  An example would be where you might read a char value from some place, and depending upon the value of the char, you need to call some particular function.  You could do a big switch statement or a series of if-else statements to decide which function to call.  Or you could create an array of 256 function pointers and assign the each pointer the function to call that corresponds to the desired function.  Then, rather than trying to interrogate the value and figure out which function to call, simply call the function directly from the array.
Guy Hengel [angelIII / a3]Connect With a Mentor Billing EngineerCommented:
in short:
first one the "pointer" part is for the returned data (int),
second one it's the function that is being pointed to.

explained here:
a function pointer (argument/parameter) gives you the possibility to specify which function you want to call (in the above, you could call as SIN or COS function, though in the called procedure you don't know what function you will be passed, except it's interface (arguments and return types)
is there any particular reason that you are re-posting information that was already given?

I thought I was simply expanding on the basic information given by the 1st responder.

However, I must admit that I did NOT read the referenced wiki article... so I apologize if the information I provided was simply a duplicate of the 1st response and the article.
sarabandeConnect With a Mentor Commented:
to add to above comments:

function to a pointer'
there is a misnomer with the term 'function to a pointer'. while a pointer can point to  a function there is nothing like a 'function to a pointer'.

int *function_pointer() - This is a function pointer.
no. this is not a function pointer it is a function returning a pointer as explained by angelIII and HooKooDooKu. you better would put the asterisk * to the return type like in

int* func_returning_an_int_ptr()

Open in new window

syntactically it is equivalent to your definition but you better would see that the * belongs to the return type and not to the function.

In C/C++ programming, functions that return a pointer are quite common.

i would like to object. in c++ returning a non-const pointer from a function is regarded as bad programming beside of a few exceptions. that is on the one hand because of encapsulation principle where a class or function should not expose its internals without need. on the other hand pointers should be created and deleted (allocated and freed) at same level of logical hierarchy. creating a pointer in one function and freeing it somewhere else is dangerous and makes it difficult to keep responsibility for allocation and deallocation. also, if a class has a (member) pointer it normally would create it in the constructor and delete it in the destructor. temporary variables normally would be created on the stack (as objects) and not on the heap (as pointers) and passed by reference and not by pointer. exceptions to that are baseclass pointers, function pointers, or smart pointers, but rarely ordinary pointers. in c you cannot pass by reference and therefore pointers are more common. but even there you would try to keep the responsibility for a pointer within one function and malloc would be the only function which returns pointers. you often can see code like the following:

typedef struct tagpoint
     double x;
     double y;
} point;

void sample_func()
     point A = { 0 };
     point B = { 0 };
     calculate(&A, &B);

Open in new window

you see that the points were created as objects and passed as pointers. that is a good way to ensure that neither there is a leak because of not freeing heap memory, nor a null pointer or invalid pointer was passed, nor that you have to care for freeing the memory at all, nor you would accidently use an already freed pointer.

In C/C++ programming, function pointers are relatively rare.

i agree though there are some concepts or design patterns which are dependent on function pointers. an example beside of the callback function pointers already mentioned is threading. when creating a thread you always pass the thread function to be executed by function pointer. an other example is the factory pattern which can be implemented with function pointers.

Or you could create an array of 256 function pointers and assign the each pointer the function to call that corresponds to the desired function.

if using an array of function pointers all the functions stored in the array must have the same signature (return type and arguments). even math functions which are often used as an example have different number of operands or return type. that makes it difficult to implement a useful code without casting function pointers to same prototype and store information about the "right" signature somewhere else. in c++ you could use pointers/references to baseclasses for arguments or return type and use virtual functions to resolve different functions with a common prototype. but, if using such a complex design, it probably is easier and more straight-forward to use the virtual functions directly and not go the way with using function pointers.  

searchsanjaysharmaAuthor Commented:
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.