Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the point of function pointers?

I have trouble seeing the utility of function pointers. I guess it may be useful in some cases (they exist, after all), but I can't think of a case where it's better or unavoidable to use a function pointer.

Could you give some example of good use of function pointers (in C or C++)?

like image 654
gramm Avatar asked Apr 07 '10 11:04

gramm


People also ask

Why do we use function pointer in C?

Master C and Embedded C Programming- Learn as you go Function Pointers point to code like normal pointers. In Functions Pointers, function's name can be used to get function's address. A function can also be passed as an arguments and can be returned from a function.

Are function pointers good?

No, they're not evil. They're absolute necessary in order to implement various features such as callback functions in C. Without function pointers, you could not implement: qsort(3)

What is function pointer explain with an example?

Function Pointer Syntax void (*foo)( int ); In this example, foo is a pointer to a function taking one argument, an integer, and that returns void. It's as if you're declaring a function called "*foo", which takes an int and returns void; now, if *foo is a function, then foo must be a pointer to a function.


2 Answers

Most examples boil down to callbacks: You call a function f() passing the address of another function g(), and f() calls g() for some specific task. If you pass f() the address of h() instead, then f() will call back h() instead.

Basically, this is a way to parametrize a function: Some part of its behavior is not hard-coded into f(), but into the callback function. Callers can make f() behave differently by passing different callback functions. A classic is qsort() from the C standard library that takes its sorting criterion as a pointer to a comparison function.

In C++, this is often done using function objects (also called functors). These are objects that overload the function call operator, so you can call them as if they were a function. Example:

class functor {   public:      void operator()(int i) {std::cout << "the answer is: " << i << '\n';} };  functor f; f(42); 

The idea behind this is that, unlike a function pointer, a function object can carry not only an algorithm, but also data:

class functor {   public:      functor(const std::string& prompt) : prompt_(prompt) {}      void operator()(int i) {std::cout << prompt_ << i << '\n';}   private:      std::string prompt_; };  functor f("the answer is: "); f(42); 

Another advantage is that it is sometimes easier to inline calls to function objects than calls through function pointers. This is a reason why sorting in C++ is sometimes faster than sorting in C.

like image 136
sbi Avatar answered Sep 29 '22 05:09

sbi


Well, I generally use them (professionally) in jump tables (see also this StackOverflow question).

Jump tables are commonly (but not exclusively) used in finite state machines to make them data driven. Instead of nested switch/case

  switch (state)      case A:        switch (event):          case e1: ....          case e2: ....      case B:        switch (event):          case e3: ....          case e1: .... 

you can make a 2d array of function pointers and just call handleEvent[state][event]

like image 45
Mawg says reinstate Monica Avatar answered Sep 29 '22 05:09

Mawg says reinstate Monica