Thanks to everyone on this forum, I normally find my answers without needing to post.
I am creating a menu for an amplifier, I could do it with ‘if’ statements but came across function pointer arrays somewhere… I am using a rotary encoder with a button and have a few menus so this would be an ideal solution. I have tried using a typedef for the array but that would not work either.
I get this error message - cannot convert ‘PreampCore1::settingsMenu’ from type ‘void (PreampCore1::)()’ to type 'void ()()’
void ([4])(){(), (), (), ()}
void inputSelect(){};
void balanceSet(){};
void settingsMenu(){};
void menu()
{
int8_t totalOptions = 5;
int8_t totalFunctions = 4; // only 4 because we have a BACK as last option
int8_t menuPos = 1;
const char *menuOptions[] =
{// the options in this menu
"STANDBY",
"INPUT",
"BALANCE",
"SETTINGS",
"BACK"};
void (*menuFunctions[])() =
{// function for each option
Standby,
inputSelect,
balanceSet,
settingsMenu}; // error squiggly is here
int8_t i;
i = uiMenu(totalOptions, menuOptions, menuPos); // returns the users selection
if (i < totalFunctions)
{
(*menuFunctions[i])(); // calls the function
}
return; // not needed :)
}
int8_t uiMenu(int8_t totalOptions, const char *menuOptions[], int8_t menuStartPos)
{ // not coded yet
int i;
return i;
};
I based my code on this example. Declare an array of pointers to functions in Visual C++. It was the simplest example I could find in C++ just noticed it is Visual.
/*
* Compile options needed: none
*/
#include <stdio.h>
void test1();
void test2(); /* Prototypes */
void test3();
/* array with three functions */
void (*functptr[])() = { test1, test2, test3 } ;
void main()
{
(*functptr[0])(); /* Call first function */
(*functptr[1])(); /* Call second function */
(*functptr[2])(); /* Call third function */
}
void test1()
{
printf("hello 0\n");
}
void test2()
{
printf("hello 1\n");
}
void test3()
{
printf("hello 2\n");
}