A function pointer is a variable that stores the address of a function that can later be called through that function pointer.
function pointers is setting up "listener" or "callback" functions that are invoked when a particular event happens. The function is called, and this notifies your code that something of interest has taken place.
Why would you ever write code with callback functions? You often see it when writing code using someone's library. One example is when you're writing code for a a graphical user interface (GUI). Most of the time, the user will interact with a loop that allows the mouse pointer to move and that redraws the interface. Sometimes, however, the user will click on a button or enter text into a field. These operations are "events" that may require a response that your program needs to handle. How can your code know what's happening? Using Callback functions! The user's click should cause the interface to call a function that you wrote to handle the event.
To get a sense for when you might do this, consider what might happen if you were using a GUI library that had a "create_button" function. It might 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. Assuming for the moment that C (and C++) had a generic "function pointer" type called function, this might look like this:
void create_button( int x, int y, const char *text, function callback_func );
Whenever the button is clicked, callback_func will be invoked. Exactly what callback_func does depends on the button; this is why allowing the create_button function to take a function pointer is useful.
#include <stdio.h>
void my_int_func(int x)
{
printf( "%d\n", x );
}
int main()
{
void (*foo)(int);
/* the ampersand is actually optional */
foo = &my_int_func;
/* call my_int_func (note that you do not need to write (*foo)(2) ) */
foo( 2 );
/* but if you want to, you may */
(*foo)( 2 );
return 0;
}
Example of check numeric value using function pointer.
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
void check(char *a, char *b, int (*cmp) (const char *,
const char *));
int numcmp(const char *a, const char *b);
int main(void)
{
char s1[80], s2[80];
gets(s1);
gets(s2);
if(isalpha(*s1))
check(s1, s2, strcmp);
else
check(s1, s2, numcmp);
return 0;
}
void check(char *a, char *b, int (*cmp) (const char *,
const char *))
{
printf("Testing for equality.\n");
if(!(*cmp) (a, b)) printf("Equal");
else printf("Not equal");
}
int numcmp(const char *a, const char *b)
{
if(atoi(a)==atoi(b)) return 0;
else return 1;
}
You can often avoid the need for explicit function pointers by using virtual functions. For instance, you could write a sorting routine that takes a pointer to a class that provides a virtual function called compare:
Benefits of Function Pointers
Function pointers provide a way of passing around instructions for how to do something
You can write flexible functions and libraries that allow the programmer to choose behavior by passing function pointers as arguments
This flexibility can also be achieved by using classes with virtual functions
References:
1) http://www.cprogramming.com/tutorial/function-pointers.html
2) C++ reference fourth edition
3) MSDN
No comments:
Post a Comment