Struggling with pointers a bit...
Trying to create a menu within one of my projects, first step is a menu function that has a set of choices that you toggle between, then after selecting it will return the option selected.
It all works fine when I use the typed reference to the list I want: &(menu_mainTable[currRecord]))
This list has to come from the input to the function, which is where I'm struggling...
It needs to point to the values in the input to the function.
I'm sure its an easy answer, but some explanation would be appreciated!
#include <avr/pgmspace.h>
prog_char menu_1[] PROGMEM = "option1";
prog_char menu_2[] PROGMEM = "option2";
prog_char menu_3[] PROGMEM = "option3";
char buffer[8];
PROGMEM const char *menu_mainTable[] =
{
menu_1,
menu_2,
menu_3
};
byte menu(const char* inputs) {
byte currRecord = 0;
byte maxValue = 2; //set as function input later
while(true) { //not selected
strcpy_P(buffer, (char*)pgm_read_word([b][u]&(menu_mainTable[currRecord]))[/u][/b]);
Serial.println( buffer ); //display currently viewed option
while(readButton1() == false) { //wait for change request
if (checkKeyPad()== 0x0A) { //select button via keypad
return(currRecord);
}
else if(false) { //back button, for future use, no avail hardware atm
return(-1);
}
}
while(readButton1() == true) { //debounce ---- clean up later
}
delay(50); //debounce ---- clean up later
currRecord++;
if (currRecord > maxValue) currRecord = 0;
else if (currRecord < 0) currRecord = maxValue;
}
}
void menuMain() {
while(true){
byte x = menu(*menu_mainTable);
Serial.println(x); //display ouput
delay(100);
}
}
menu_mainTable IS a pointer.
*menu_mainTable is a pointer to a pointer, which is not what the function expects. Why did you include the *?
The function expects a pointer to char. menu_mainTable is NOT a pointer to char or a char array.
Oops, your thinking of a declaration. 'menu_mainTable' returns a pointer to the first element which is a char*, so the pointer returned is char**.
The * used in the function call is a dreference, not an 'address of' operator. The dereference moves the pointer returned from the array to a char*, which the function accepts.
@OP
However even though the code is valid, you are only passing the first element. You need to pass the whole array, or to stick with pointers, the pointer returned by the array.
Hint: Arrays aren't pointers, they implicitly cast / decay to pointers of their first element. Here is a changed version of your code ( missing stuff stripped )