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

Posted on 2014-04-14
Medium Priority
Last Modified: 2014-05-08
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.
Question by:searchsanjaysharma
LVL 143

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 501 total points
ID: 40001244
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)
LVL 16

Accepted Solution

HooKooDooKu earned 501 total points
ID: 40001535
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.
LVL 16

Expert Comment

ID: 40004648
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.
LVL 35

Assisted Solution

sarabande earned 498 total points
ID: 40015675
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.  


Author Closing Comment

ID: 40052674

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
Suggested Courses
Course of the Month14 days, 19 hours left to enroll

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question