Richard Tobin wrote:
In article <Xs*****************@nnrp.ca.mci.com!nnrp1.uunet.c a>,
Clever Monkey <sp******@clevermonkey.org.INVALIDwrote:
>>>I am looking for some comprehensive tutorials on how to write call
back functions in C. Can anyone help me?
>> Write a callback function in the same way you write
any other function. There is nothing "different" about a
callback function, nothing at all. It's just a function.
>That is to say, the "callback" part is how the function is used, not how
it is defined.
Not entirely. The way that it is used has implications for its
definition.
Callback functions are unusual in that their types are generally
specified by a library, but their definitions are written by the user,
and some or all of their arguments are passed in by the user. As a
result, callbacks are commonly defined to take void * arguments
(allowing the user some flexibility in the values that can be passed)
where a similar function not used as a callback would probably take
more specifically typed arguments.
I see your point. Upon consideration, I realize I have experience only
with two varieties of callback:
- An function passed to a module to facilitate code generalization.
e.g., a way to provide a function to a module that updates a UI by
calling back to an API that has no knowledge of the UI specifics.
- A generic library call, such as a data structure deletion function, or
the classic sorting function.
In the first case we generally know what sorts of args it will take, and
what type (it does in the legacy code I maintain, anyway). It is just
who is running it, and how exactly they will use it that is unknown to
the provider -- the provider just provides the data in a known format.
In the latter we have to provide a more generic interface.
I'm more familiar with the former, but recently had to implement a map
for a public API, which required a generic dispose() function to be used
as a callback. I'd forgotten the subtleties.
--
clvrmnky <mailto:sp******@clevermonkey.org>
Direct replies will be blacklisted. Replace "spamtrap" with my name to
contact me directly.