what is the use of function pointer?

Plz help me m getting this question in all interview

What is the use of function pointer?
My ans: Function pointer is replacement of switch


But interviewer laughed and he said its not the exact answer

pla help i got this question in 3 more interviews .
Wait, are you trying to get a job in the field? In that case, you seriously should open your browser, type http://www.google.com

and figure that out on your own. I understand if a complete beginner has problems with this, but if you're trying to work with this lack of knowledge (and lack of initiative to find out)...

Hi hanst99,
i used function pointer in thread programming

pthread_create(&tid,NULL,routine,NULL);

in this routine is function address which m passing in this API
Function pointers - and more generally, function objects - can be used for numerous things, obviously for threads, but you can use them pretty much anywhere where *a* function is supposed to be called and it's less important what that function actually does. Example applications would be callbacks, stream manipulators, generic algorithms etc etc.
You use function pointer for indirect calls. Indirect means that the caller doesn't know the actual function and what it does. Which is the reason that's usually rather not recommended...
Which is the reason that's usually rather not recommended...
Who does not recommend it?
closed account (D80DSL3A)
Perhaps an example will show why a function pointer is more than a substitute for a switch statement.
Here is a function which approximates the definite integral of any function of a single variable.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// takes a pointer to a function serving as the integrand
double RiemannSum( double a, double b, double (*pIntegrand)(double), int n )
{
	double sum = 0.0;	
	double subInt = (b-a)/static_cast<double>(n);
	double x = a + subInt/2.0;

	for(int k=0; k<n; k++)
	{
		sum += pIntegrand( x )*subInt;// The function being integrated is called through a pointer here.
		x += subInt;
	}

	return sum;
}

Here, a and b are the integration limits and n is the # of sub-intervals for the approximation.

The advantage (in using a function pointer) is that I can write any function I wish, such as:
 
double f(double x){ return pow(x,1.73); }

Then find the integral of f(x) from 1.0 to 4.0 by calling:
RiemannSum( 1.0, 4.0, f, 1000 )// approximating over 1,000 sub-intervals

This flexibility can't be achieved with a switch statement.
*cough*
 
std::cout<<std::endl;


Spot the function pointer.
Last edited on
hanst99 wrote:
Who does not recommend it?
Sigh: most likely the one who said it
the caller doesn't know the actual function and what it does.
¿? The function that receives the function pointer does not care (or it should not) about that. That is the beauty.
Look at the example proposed by fun2code, you do know what function you want to integrate (because you passed it)

By the way, this really bothers me
1
2
3
4
5
template < class InputIterator, class OutputIterator, class UnaryOperator >
OutputIterator transform ( InputIterator first1, InputIterator last1, OutputIterator result, 
                        UnaryOperator op ); //accept function pointers and functors

valarray<T> apply (T func(T)) const; //just function pointers 
Last edited on
@ hanst99: Is that a question for the OP or is it open to the floor?

@ OP: As some more examples of where you use a function pointer (they are all over Win32): The 'lpfnWndProc' property in a Win32 WNDCLASSEX sturct is actually a function pointer. You pass this to the OS so that it knows what function in your program to call in order to process the messages for your Window Class. The same with the 'ThreadProc' for the CreateThread() function and every transacted function uses a function pointer passed to it as an argument to designate the callback function in your application.

Another use for a function pointer, although it is sloppy and should never be done in a professional setting, is to address a function that is not listed in a libraries import table or in other words a function that is not exported. Again this should never actually be done but it is a good way to illustrate that a function pointer is literally the address of the function, just like a regular pointer is the address of a variable.
It was more or less a poke at fun2code saying that using function pointers is "usually rather not recommended".
closed account (D80DSL3A)
@hanst99: You're poking the wrong guy! Check again.
You are correct, but I still feel like poking you.
closed account (D80DSL3A)
Fine. Poke away.
I've missed your less-than-subtle charm the last few months. Where did you go?

By the way... the '99' in your username. Is that your birth year (1999)?
Last edited on
I could pretend that I was busy, but I was actually just taking a break from programming (not completely, but I really didn't do very much) before entering university - I figured if I was going to do this nonstop for the next 5 years, and probably most of the rest of my life, I might as well take it easy for a while.

Actually, that was a lie too. I was trying to get into game programming, and hit so many distractions on the way that I didn't get one thing done, and just ragequitted. Fortunately I am fully recovered now.

Enough with the off topic discussions though :P
Topic archived. No new replies allowed.