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;
void fB(void *arg) {

	// Cast as a pointer to a string of chars.
	char * str = (char*)arg;
void fC(void *arg) {
	Serial.println("fC:Called with no parameter.");

void setup() {
	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!";
	// Calling a function with no parameter.
	// This call also work : (*f[2])(0).

void loop() {}

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.