?
Solved

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

Posted on 2014-04-14
7
Medium Priority
?
5,924 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
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:
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 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.
0
 
LVL 16

Expert Comment

by:HooKooDooKu
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.
0
 
LVL 35

Assisted Solution

by:sarabande
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.  

Sara
0
 

Author Closing Comment

by:searchsanjaysharma
ID: 40052674
tx
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
Suggested Courses
Course of the Month9 days, 19 hours left to enroll

762 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