Puchi said:
I have been trying to understand function pointers for a while but not
able to figure out why we really need a function pointer. I understand
what it does but why do we need them, why cant we call the function
itself.
In OOP, function pointer aren't needed, it's not OOP.
In "classic" C, it's very useful, look at the qsort function for
instance: you want to sort an array, how to give the sort criteria
(numéric, date, character based, color, whatever) in a generic way? The
idea is to give the array to sort and the function used to compare two
element of this array as parameter of the sort function.
Internally, virtual member of a class are kind of function pointer. In
fact, lot of C application use function pointer to mimic some OOP
concept.
Note: The big issue with function pointer is that they're not typed,
it's very very easy to produce compilable but totally incorrect code and
hard to debug using function pointer. If you want to get an idea of a
possible better solution, look at C# (.NET) delegates.