This is not my code but something I started playing around with in a search for a solution to my array of pointers to function. The following compiles without errors. Scroll down for the second version which IMOSHO should also compile but doesn't
typedef void (* GenericFP)(int); //function pointer prototype to a function which takes an 'int' an returns 'void'
GenericFP MenuFP[3] = {&Page1, &Page2, &Page3}; //create an array of 'GenericFP' function pointers. Notice the '&' operator
void setup()
{
Serial.begin(115200);
}
void loop()
{
MenuFP[0](1); //call MenuFP element 0 with the parameter 1. This is equivalent to: Page1(1)
MenuFP[1](5); //call MenuFP element 1 with the parameter 5. This is equivalent to: Page2(5)
MenuFP[2](7); //and so on. This is equivalent to: Page3(7)
}
void Page1(int foo)
{
Serial.print("Page1=");
Serial.println(foo);
}
void Page2(int foo)
{
Serial.print("Page2=");
Serial.println(foo);
}
void Page3(int foo)
{
Serial.print("Page3=");
Serial.println(foo);
}
----------------------------second version----------------------------------------------------
typedef void (* GenericFP)(int); //function pointer prototype to a function which takes an 'int' an returns 'void'
GenericFP MenuFP[3] = {&Page1,
&Page2, &Page3}; //create an array of 'GenericFP' function pointers. Notice the '&' operator
void setup()
{
Serial.begin(115200);
}
void loop()
{
MenuFP[0](1); //call MenuFP element 0 with the parameter 1. This is equivalent to: Page1(1)
MenuFP[1](5); //call MenuFP element 1 with the parameter 5. This is equivalent to: Page2(5)
MenuFP[2](7); //and so on. This is equivalent to: Page3(7)
}
void Page1(int foo)
{
Serial.print("Page1=");
Serial.println(foo);
}
void Page2(int foo)
{
Serial.print("Page2=");
Serial.println(foo);
}
void Page3(int foo)
{
Serial.print("Page3=");
Serial.println(foo);
}
The only difference is the MenuFP array initialization. I put a new-line between &Page1 and &Page2. My understanding is this should be considered "white space".