Pointers to Functions in C

Hi
What is the point of using pointers to functions in C ?
Why not call directly a function ?

Thanks
Elico

So that you can pass a pointer to an arbitrary function to another function, so that you can form arrays of functions, so that C++ can work . . .

Think of the game Monopoly and how you might code landing on each square. Now frame that same design using pointers to functions. With a little thought, you'll see that pointers to function can simplify things considerably.

So that you can pass a pointer to an arbitrary function to another function

Common subset of this: Callbacks.

wildbill:

So that you can pass a pointer to an arbitrary function to another function

Common subset of this: Callbacks.

Example of this subset:

You've coded a library that reads Serial data and performs an action when a specific delimiting character is found. As the library developer, you don't know what the user wants to do with the data, so you allow the user to set a callback function that gets called whenever that delimiting character is found.

I created an image processing function parser for ImageFX on the Amiga. I used lexx and Yacc to parse the expressions which resulted in a series of operations to perform. I used function pointers so that I could save everything on a stack and call things directly rather than having a huge switch/case.

look at this signature of quicksort()

void qsort (void* base, size_t num, size_t size, int (compar)(const void,const void*));

the last argument is a pointer to a compare function.
This way quicksort can sort any datatype!

-qsort - C++ Reference -