Array of function pointers with mixed parameters
This is my work to create an array of function pointers which they can accept one parameter of any kind.
/*
2010-10-18: Basics of array of function pointers.
The trick here is to make these functions accept different
types of parameters using a void pointer.
*/
// Define the function pointer type.
// The void pointer can be casted to any type of value.
typedef void(*fctPtr)(void *);
// Declare the functions.
void fA(void *arg);
void fB(void *arg);
void fC(void *arg);
// Declare an array of functions pointers.
fctPtr f[3] = {fA,fB,fC}; // Array of function name (the pointer to that name).
// The functions.
void fA(void *arg) {
// Cast as a pointer to an int and assign it's value to r.
int r = *(int*)arg;
Serial.print("fA:");
Serial.println(r);
}
void fB(void *arg) {
// Cast as a pointer to a string of chars.
char * str = (char*)arg;
Serial.print("fB:");
Serial.println(str);
}
void fC(void *arg) {
Serial.println("fC:Called with no parameter.");
}
void setup() {
Serial.begin(9600);
delay(1000); // Wait a second.
int a = 123;
(*f[0])(&a); // Call the first function of the array of functions.
char b[] = "This is a test!";
(*f[1])(&b);
// Calling a function with no parameter.
(*f[2])(NULL);
// This call also work : (*f[2])(0).
}
void loop() {}

