Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Callback Function in C 2

Status
Not open for further replies.

SwapSawe

Programmer
Apr 24, 2001
148
US
Can you help me regarding callback() function, how it is used and what its for.
 
That's a pretty general question. Basically, a callback function is used to generalize a function so that it can performed a user-defined (user in the sense of a "user programmer" that uses the function) function on a set of data.

qsort() uses this mechanism where the user creates a comparison function (4th argument to qsort()) which qsort() calls as needed to sort the array elements.

In C, callback functions are implemented with function pointers.

Russ
bobbitts@hotmail.com
 
Hey Robbitt
Thanx, This was extremely helpful can u help me with a demo-code.


Regards.
SwapSawe.
 
Call back prototype declaration:
typedef int (*My_function)(int in_iData);

Function with call back declaration:
int func(My_function fCallBack, int in_PassData)
{
fCallBack(in_PassData);
}

Call back function itself:
int PrintData(in_iData)
{
printf("Data:%d\n",in_iData);
}

Call back use:

func(PrintData, 5);
this will print "Data:5".
Good luck.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top