Solved

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

Posted on 2014-04-14
7
4,753 Views
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.
0
Comment
Question by:searchsanjaysharma
7 Comments
 
LVL 142

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 167 total points
Comment Utility
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:
http://en.wikipedia.org/wiki/Function_pointer#Simple_function_pointers
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)
0
 
LVL 16

Accepted Solution

by:
HooKooDooKu earned 167 total points
Comment Utility
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.
0
 
LVL 16

Expert Comment

by:HooKooDooKu
Comment Utility
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.
0
 
LVL 32

Assisted Solution

by:sarabande
sarabande earned 166 total points
Comment Utility
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.  

Sara
0
 

Author Closing Comment

by:searchsanjaysharma
Comment Utility
tx
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
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…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now