Pages: [1]   Go Down
Author Topic: Pointers to Functions in C  (Read 186 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Full Member
***
Karma: 2
Posts: 158
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Thanks
Elico
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 302
Posts: 26248
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 . . .
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Cincinnati, OH
Offline Offline
God Member
*****
Karma: 43
Posts: 775
I'm not bossy...I just know what you should be doing.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

New Jersey
Offline Offline
Faraday Member
**
Karma: 67
Posts: 3679
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Common subset of this: Callbacks.
Logged

California
Offline Offline
Faraday Member
**
Karma: 88
Posts: 3379
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.
Logged

Offline Offline
Edison Member
*
Karma: 33
Posts: 1438
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 216
Posts: 13673
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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!

-http://www.cplusplus.com/reference/cstdlib/qsort/ -
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Pages: [1]   Go Up
Jump to: