Function Pointers in C and C++ - Cprogramming
Function Pointers in C and C++ - Cprogramming
By Alex Allain
Callback Functions
Another use for function pointers is setting up "listener" or "callback" functions that are inv oked when a particular ev ent happens. The function is called, and this notifies y our code that som ething of interest has taken place. Why would y ou ev er write code with callback functions? You often see it when writing code using som eone's library . One exam ple is when y ou're writing code for a graphical user interface (GUI). Most of the tim e, the user will interact with a loop that allows the m ouse pointer to m ov e and that redraws the interface. Som etim es, howev er, the user will click on a button or enter text into a field. These operations are "ev ents" that m ay require a response that y our program needs to handle. How can y our code know what's happening? Using Callback functions! The user's click should cause the interface to call a function that y ou wrote to handle the ev ent. To get a sense for when y ou m ight do this, consider what m ight happen if y ou were using a GUI library that had a "create_button" function. It m ight take the location where a button should appear on the screen, the text of the button, and a function to call when the button is clicked. Assum ing for the m om ent that C (and C+ + ) had a generic "function pointer" ty pe called function, this m ight look like this: v o i dc r e a t e _ b u t t o n (i n tx ,i n ty ,c o n s tc h a r* t e x t ,f u n c t i o nc a l l b a c k _ f u n c) ; Whenev er the button is clicked, callback_func will be inv oked. Exactly what callback_func does depends on the button; this is why allowing the create_button function to take a function pointer is useful.
www.cprogramming.com/tutorial/function-pointers.html
1/5
3/29/13
In this exam ple, foo is a pointer to a function taking one argum ent, an integer, and that returns v oid. It's as if y ou're declaring a function called "*foo", which takes an int and returns v oid; now, if *foo is a function, then foo m ust be a pointer to a function. (Sim ilarly , a declaration like int *x can be read as *x is an int, so x m ust be a pointer to an int.) The key to writing the declaration for a function pointer is that y ou're just writing out the declaration of a function but with (*func_nam e) where y ou'd norm ally just put func_nam e.
i n tm a i n ( ) { v o i d( * f o o ) ( i n t ) ; f o o=& m y _ i n t _ f u n c ; / *c a l lm y _ i n t _ f u n c( n o t et h a ty o ud on o tn e e dt ow r i t e( * f o o ) ( 2 ))* /
www.cprogramming.com/tutorial/function-pointers.html 2/5
3/29/13
f o o (2) ; / *b u ti fy o uw a n tt o ,y o um a y* / ( * f o o ) (2) ; r e t u r n0 ; } Note that function pointer sy ntax is flexible; it can either look like m ost other uses of pointers, with & and *, or y ou m ay om it that part of sy ntax. This is sim ilar to how array s are treated, where a bare array decay s to a pointer, but y ou m ay also prefix the array with & to request its address.
3/29/13
f o r(i=0 ;i<1 0 ;+ + i) { a r r a y [i]=1 0-i ; } q s o r t (a r r a y ,1 0,s i z e o f (i n t) ,i n t _ s o r t e r) ; f o r(i=0 ;i<1 0 ;+ + i) { p r i n t f(" % d \ n ", a r r a y [i]) ; } }
3/29/13
pointers--it just happens that the com piler m akes the work easier for y ou. Using poly m orphism can be an appropriate strategy (for instance, it's used by Jav a), but it does lead to the ov erhead of hav ing to create an object rather than sim ply pass in a function pointer.
www.cprogramming.com/tutorial/function-pointers.html
5/5