"Tuxpal" <su***********@ rediffmail.com> wrote in message
What are the pros/cons of using Callback mechanism in C?
(Design perspective)
What are the scenarios where it can be applied ?
Callbacks are usually used to leave some functionality out of a module, to
be provided by the caller. A standard library example is qsort(). Another
example would be if you implement some sort of database structure, and
require the caller to provide a getkey() function to retrieve the key from
the data.
The advantage is that the functions using callback are much more general.
qsort() for instance can sort any flat array without being recompiled.
One disadvantage is that providing the callback puts quite a burden on the
caller - you have to know exactly what the comparison function provided to
qsort() is expected to return, for example. If you accidentally return
order-dependent results for two items that in fact compare as equal then the
call could strangely fail just before shipping.
Another disadvantage is that it is harder to write a call-tree of a program
that uses function pointers. For short callbacks this isn't too much of a
problem, but if you use them too heavily you can no longer have a structured
program. This can be a difficulty in Windows programming, for example.