callback functions

D

DevarajA

Is it possible to write a function that, once called, is executed
together with the caller without stopping it? I've seen such functions
in windows programming, using CALLBACK qualifier before the function
name (is it a macro?). Now I would like to know if this could be
possible in standard C, maybe using some tricks. And going a little ot..
what other non-standard ways to do that do you know?
 
E

Emmanuel Delahaye

DevarajA wrote on 03/09/05 :
Is it possible to write a function that, once called, is executed together
with the caller without stopping it? I've seen such functions in windows
programming, using CALLBACK qualifier before the function name (is it a
macro?). Now I would like to know if this could be possible in standard C,
maybe using some tricks. And going a little ot.. what other non-standard ways
to do that do you know?

Callbacks have nothing to do with concurrent ('executed together ')
functions. They are just user functions called by some lower level
function (system, library...) See the standard qsort() function and its
'compare' callback.

If you want concurrent (well, sort of) execution, you want processes or
threads. Theses are beyond the scope of the C-language. Ask a newsgroup
dedicated to your system.

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"Clearly your code does not meet the original spec."
"You are sentenced to 30 lashes with a wet noodle."
-- Jerry Coffin in a.l.c.c++
 
J

John Smith

Emmanuel said:
DevarajA wrote on 03/09/05 :



Callbacks have nothing to do with concurrent ('executed together ')
functions. They are just user functions called by some lower level
function (system, library...) See the standard qsort() function and its
'compare' callback.

If you want concurrent (well, sort of) execution, you want processes or
threads. Theses are beyond the scope of the C-language. Ask a newsgroup
dedicated to your system.

You define a callback function as a user function called by a
lower level function, but the example you give seems to
illustrate the opposite case, that is, one in which a user
function calls a lower level function. qsort always calls
compare, never the other way round. Have I misunderstood?

JS
 
E

Emmanuel Delahaye

John Smith wrote on 03/09/05 :
You define a callback function as a user function called by a lower level
function, but the example you give seems to illustrate the opposite case,

I don't think so.
that is, one in which a user function calls a lower level function. qsort
always calls compare, never the other way round. Have I misunderstood?

This is exactly how the compare() function is called : by qsort() which
is a library function.

user code ------|--> qsort() (RTL code)
compare() <---|---

The compare() function belongs to the user's world.

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"There are 10 types of people in the world today;
those that understand binary, and those that dont."
 
D

DevarajA

Emmanuel Delahaye ha scritto:
John Smith wrote on 03/09/05 :



I don't think so.



This is exactly how the compare() function is called : by qsort() which
is a library function.

user code ------|--> qsort() (RTL code)
compare() <---|---

What do you mean by RTL code?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top