Possible Duplicate:
What is the point of function pointers?
I just read about pointers to function in K+R. I understand how these pointers work, but I don't see the utility for they. What's the use for this type of pointers in programming?
Possible Duplicate:
What is the point of function pointers?
I just read about pointers to function in K+R. I understand how these pointers work, but I don't see the utility for they. What's the use for this type of pointers in programming?
One of the main reasons for using pointers is for when we pass data around. If we use pointers we won't be making senseless amount of copies of the same data.
For example, you want to pass a structure that is 200 bytes in size to a function. What's more expensive: passing the structure's 4 (or 8) byte memory address or the entire copy of the 200 bytes?
You can use function pointers for a variety of tasks such as registering factories, applying an algorithm on a data structure (e.g. sorting a list using a user-defined sort function). There are a few other uses but essentially you need them for inversion of control: let someone else call back into your code, and you do this by providing a pointer to the function.
There are lots of uses, but in my personal experience they are most useful when manipulating arrays since C arrays are kinda hairy.