Diff: CastingPointerToFunction

Differences between current version and predecessor to the previous major change of CastingPointerToFunction.

Other diffs: Previous Revision, Previous Author, or view the Annotated Edit History

Newer page: version 13 Last edited on Monday, May 15, 2006 2:48:05 pm by AristotlePagaltzis
Older page: version 3 Last edited on Sunday, November 10, 2002 8:58:56 pm by CraigBox Revert
@@ -8,18 +8,48 @@
 The secret here is the parenthesis around the star. To declare a variable to hold the above, use this: 
  int (*func_t)(char *); 
-Another trick is:  
- typedef int (*func_t)(char *); 
+In C (and [C++]), a function name is really just a pointer to a function, and you can think of () as an operator to make a call to that function. Using a typedef to hold the function type might make that clear :  
+ typedef int (*func_t)(char *); 
  int foo(char *x) { 
- func_t p = foo;  
- return p("Narf!"); 
+ func_t p = foo; /* assign function pointer to p */  
+ return p("Narf!"); /* call that function, and return the result */  
 And you need to do this why? 
+* Jon was using it to "compile" something then jump to it in C, by casting an array as a function, then calling it.  
+* I use it frequently to do things like have a lookup table of name to function. For example in ircu, there is a function called "parse" which takes a line from a user, splits it up into a function and it's arguments, then scans through a table of functions and their names, when it finds one that matches that command, it calls the function with the arguments it parsed earlier. Nice 'n zippy.  
+* A library back-end may provide functionality that should behave differently for different front ends. For example, a library that wants to print out diagnostic messages uses a function pointer, and then a console app can pass a pointer to a function that prints out to the terminal (or stdout(3) or stderr(3)) while a graphical app can create a callback function that pops up a [GUI] window or something.  
+* Some functions take another function as an argument. Probably the most well-known example is qsort(3) the quick sort algorithm -- you need to pass it an array and a function for comparing array elements. Also see the example below.  
+from atexit(3):  
+ #include <stdlib.h>  
+ int atexit(void (*function)(void));  
+you can specify functions to be executed after main() finishes.  
+You do this by calling atexit(functionname), as a function name  
+without () is a pointer to that function. However, if you want to this for a function that returns something other than null, you have to type cast it. Eg:  
+ (int endwin() is an ncurses function).  
+ atexit(endwin);  
+ source.cpp:227: passing `int (*)()' as argument 1 of `atexit(void (*)())'  
+ atexit(void)endwin);  
+ source.cpp:227: void value not ignored as it ought to be  
+The trick is to cast the pointer to a function pointer of the required type:  
+ atexit( (void(*)())endwin );  
+It is worth noticing that in languages where functions are first class objects (such as [LISP] and [Scheme]), passing around pointers to functions is entirely normal (and type safe); even in [Java] and [Python] [Introspection] allows type safe access to such functionality.