I discovered this site today and found it useful.
One example I got running on Arduino:
[edit: Oops, From another site http://www.codeterrorizer.com/cc/using-function-pointers-function-pointer-part-ii]
#include <stdio.h>
#include <Serial.h>
/**
* Using a function pointer in a structure.
*/
struct _Object {
void (* functionPointer)();
int value;
};
/**
* Passing a function pointer to a function.
*/
void passFunction(void (* functionPointer)()) {
/* call our passed function */
functionPointer();
}
void testFunction() {
Serial.println("hello");
}
void testFunction2() {
Serial.println("juhu");
}
void setup() {
Serial.begin(9600);
/* pointer to void function with no parameters myFunction */
void (* myFunctionPointer)() = testFunction;
/* call original function */
Serial.print("#1 ");
testFunction();
/* call our new pointer function */
Serial.print("#2 ");
myFunctionPointer();
/* create struct */
struct _Object Object;
Object.value = 1;
Object.functionPointer = testFunction;
Serial.print("#3 ");
printf("%d\n", Object.value);
Serial.print("#4 ");
Object.functionPointer();
/* Add function pointer to our gobal array. */
int functionPointers[2];
functionPointers[0] = (int)testFunction;
Serial.print("#5 ");
printf("%d\n", functionPointers[0]);
functionPointers[1] = (int)testFunction2;
printf("%d\n", functionPointers[1]);
/* Creepy! but possible. */
Serial.print("#6 ");
((void (*)()) functionPointers[0])();
Serial.print("#7 ");
((void (*)()) functionPointers[1])();
}
void loop() {
// put your main code here, to run repeatedly:
}
Interesting, though, cases #3 and #5 do not appear to call the function (neither 'hello' nor 'juhu' is printed).
[edit, again]
I compiled the example as a Win32 console app (to see what it presumably SHOULD do) and get this output, indicating the Arduino incarnation has 'problems':
hello
hello
1
hello
19665356
19665151
hello
juhu
[edit: Still reading? OK, I changed the example so it appears to work the same as in Win32]
#include <stdio.h>
#include <Serial.h>
/**
* Using a function pointer in a structure.
*/
struct _Object {
void (* functionPointer)();
int value;
};
/**
* Passing a function pointer to a function.
*/
void passFunction(void (* functionPointer)()) {
/* call our passed function */
functionPointer();
}
void testFunction() {
Serial.println("hello");
}
void testFunction2() {
Serial.println("juhu");
}
void setup() {
Serial.begin(9600);
/* pointer to void function with no parameters myFunction */
void (* myFunctionPointer)() = testFunction;
/* call original function */
Serial.print("#1 ");
testFunction();
/* call our new pointer function */
Serial.print("#2 ");
myFunctionPointer();
/* create struct */
struct _Object Object;
Object.value = 1;
Object.functionPointer = testFunction;
Serial.print("#3 ");
Serial.println(Object.value);
Serial.print("#4 ");
Object.functionPointer();
/* Add function pointer to our gobal array. */
int functionPointers[2];
functionPointers[0] = (int)testFunction;
Serial.print("#5 "); ////////////////////////////////////// Changed
Serial.println(functionPointers[0]);
functionPointers[1] = (int)testFunction2;
Serial.print("#6 "); ////////////////////////////////////// Changed
Serial.println(functionPointers[1]);
/* Creepy! but possible. */
Serial.print("#7 ");
((void (*)()) functionPointers[0])();
Serial.print("#8 ");
((void (*)()) functionPointers[1])();
}
void loop() {
// put your main code here, to run repeatedly:
}
and this prints out:
#1 hello
#2 hello
#3 1
#4 hello
#5 103
#6 96
#7 hello
#8 juhu