I had some experience lately with function pointers in C.
So going on with the tradition of answering your own questions, I decided to make a small summary of the very basics, for those who need a quick dive-in to the subject.
Functions pointers in C
Let's start with a basic function which we will be pointing to:
First thing, lets define a pointer to a function which receives 2
Now we can safely point to our function:
Now that we have a pointer to the function, lets use it:
Passing the pointer to another function is basically the same:
We can use function pointers in return values as well (try to keep up, it gets messy):
But it's much nicer to use a
Disclaimer: this code was not necessarily compiled, if you see any errors, feel free to edit.
Function pointers in C can be used to perform object-oriented programming in C.
For example, the following lines is written in C:
By using function pointers, it is possible to emulate methods in C.
How is this accomplished?
As can be seen, the methods of the
For example, the
One thing that can be noticed is that there is no concept of an instance of an object and having methods that are actually a part of an object, so a "self object" must be passed in on each invocation. (And the
So, rather than being able to do
With that minor explanation having to pass in a reference to yourself out of the way, we'll move to the next part, which is inheritance in C.
Let's say we want to make a subclass of
Basically, for all subclasses, the available methods are once again function pointers. This time, the declaration for the
As for the implementation of the
In instantiating the
The use of a function pointer can achieve inheritance of a method from a superclass.
We can further continue to polymorphism in C.
If for example we wanted to change the behavior of the
Adding an overriding
Then, the function pointer for the
Now, rather than having an identical behavior for the
I must add a disclaimer that I am still learning how to write with an object-oriented programming style in C, so there probably are points that I didn't explain well, or may just be off mark in terms of how best to implement OOP in C. But my purpose was to try to illustrate one of many uses of function pointers.
For more information on how to perform object-oriented programming in C, please refer to the following questions:
The guide to getting fired: How to abuse function pointers in GCC on x86 machines by compiling your code by hand:
One of my favorite uses for function pointers is as cheap and easy iterators -
Function pointers become easy to declare once you have the basic declarators:
While D is another declarator built using those same rules. In the end, somewhere, it ends with
As you see, it's pretty easy to build it up using typedefs. Without typedefs, it's not hard either with the above declarator rules, applied consistently. As you see i missed out the part the pointer points to, and the thing the function returns. That's what appears at the very left of the declaration, and is not of interest: It's added at the end if one built up the declarator already. Let's do that. Building it up consistently, first wordy - showing the structure using
As you see, one can describe a type completely by appending declarators one after each other. Construction can be done in two ways. One is bottom-up, starting with the very right thing (leaves) and working the way through up to the identifier. The other way is top-down, starting at the identifier, working the way down to the leaves. I'll show both ways.
Construction starts with the thing at the right: The thing returned, which is the function taking char. To keep the declarators distinct, i'm going to number them:
Inserted the char parameter directly, since it's trivial. Adding a pointer to declarator by replacing
Return type is complete! Now, let's replace
Note that no parentheses are needed, since we want
I've called the identifier of the function
This starts at the identifier at the very left in the description of the type, wrapping that declarator as we walk our way through the right. Start with function taking
The next thing in the description (after "returning") was pointer to. Let's incorporate it:
Then the next thing was functon taking
Note the parentheses we added, since we again want that the
Now we just need to put
The nice thing
Is bottom-up or top-down better? I'm used to bottom-up, but some people may be more comfortable with top-down. It's a matter of taste i think. Incidentally, if you apply all the operators in that declaration, you will end up getting an int:
That is a nice property of declarations in C: The declaration asserts that if those operators are used in an expression using the identifier, then it yields the type on the very left. It's like that for arrays too.
Hope you liked this little tutorial! Now we can link to this when people wonder about the strange declaration syntax of functions. I tried to put as little C internals as possible. Feel free to edit/fix things in it.
Another good use for function pointers:
I just want to add a particular case where could FP be useful:
Think of a transform function or a sorting algorithm through an array. Now, you want to make it really flexible as to let the user of your function to specify the behaviour of your function by letting them pass a function as an argument.
Say, you write a sorting algorithm with the following procedural prototype:
The user of that function could specify, by passing the appropriate fn, if they want a descending or ascending ordering.
Or one can also create a transform function which goes through an array applying to each member such a API-user defined function:
The function pointer tutorial: http://www.newty.de/fpt/index.html is pretty good.
Since function pointers are often typed callbacks, you might want to have a look at type safe callbacks. The same applies to entry points, etc of functions that are not callbacks.
C is quite fickle and forgiving at the same time :)