Go Down

Topic: Array of function pointers on a mixed array[SOLVED] (Read 59 times) previous topic - next topic

myownway

Nov 26, 2014, 07:52 pm Last Edit: Nov 27, 2014, 01:33 am by myownway
Hello, it's been a while since I last visited this forum but I've been so busy.

Well, now I'm on a new project and I'm trying to make a menu to show on an LCD screen, there's no problem with that. The issue was when I found I don't know how to define a mixed array neither an array of function pointers.

So, taking this as an example, could someone help me?

Code: [Select]

class Menu{
  menu(){
    int current_item_selected = 0;
    ??????? menu[][] = {{"ITEM1", ItemOneFunction()}, {"ITEM2", ItemTwoFunction()}}; ???????
  }
  void next(){
    current_item_selected++;
  }
  void prev(){
    current_item_selected--;
  }
  String get_item_name(){
    /* Do stuff to return the string to show on lcd */
    return menu[current_item_selected][0];
  }
  void run(){
    /* Run function from array */
    ??????    run menu[current_item_selected][1];    ??????
  }
}

void ItemOneFunction(){
/* Do stuff */
}

void ItemTwoFunction(){
/* Do stuff */
}

econjack

#1
Nov 26, 2014, 08:33 pm Last Edit: Nov 26, 2014, 08:34 pm by econjack
I'm not at my system, but you can do this with an array of structures. This code is untested...

Code: [Select]

struct myStruct {
   char myItem[6];
   void (*ptrFunc)();
} myStructArray[] = {{"ITEM1", ItemOneFunction},
                    {"ITEM2", ItemTwoFunction}};

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
 
  for (int i = 0; i < sizeof(myStructArray) / sizeof(myStructArray[0]); i++) {
    Serial.println(myStructArray[i].myItem);
  }
}
void ItemOneFunction(){
/* Do stuff */
}

void ItemTwoFunction(){
/* Do stuff */
}
void loop() {
  // put your main code here, to run repeatedly:
}

myownway

#2
Nov 26, 2014, 08:46 pm Last Edit: Nov 26, 2014, 08:47 pm by myownway
Thanks, I'll look into using structs, but, could you explain me this line please?
Quote
void (*ptrFunc)();
I mean, what does '*ptrFunc' states for? what's it's use? is there any documentation of that?

myownway

#3
Nov 26, 2014, 08:56 pm Last Edit: Nov 26, 2014, 09:07 pm by myownway
Ok, nvm. I'm just dumb, that's a pointer to the function. Well, but shouldn't the function be referenced with '&'?

(I'm new to C)

EDIT:
Also, how do I call the functions in the struct?

econjack

Quote
Well, but shouldn't the function be referenced with '&'?
You could, but it's not needed. Like an array name without the brackets, a function name without the parentheses resolves to the memory address (the lvalue) of where the function resides in memory.

As for parsing complex data definitions, see my Right-Left Rule, which is nicely summarize at:

http://jdurrett.ba.ttu.edu/3345/handouts/RL-rule.html

PaulS

Quote
Also, how do I call the functions in the struct?
Code: [Select]
myStructArray[i].ptrFunc();
For some value of i.

myownway

#6
Nov 27, 2014, 12:27 am Last Edit: Nov 27, 2014, 12:33 am by myownway
Thanks, I've used that just by logic at the end. Hahaha.

Now I have another problem, this code gives me an error on the line 'menu_items = items;', I have some trouble passing the constructor's array variable to the class array variable. What can I do? Here is my code:

Code: [Select]
struct MENULIST {
  String item;
  void (*ptrFunc)();
};

MENULIST main_menu_items[] = {
{"ITEMONE", ItemOneFunction},
{"ITEMTWO", ItemTwoFunction}
};

class Menu{
  public:
    Menu(MENULIST items[]){
      current_item_selected = 0;
      menu_items = items;
    }
    void next(){
      if (((sizeof(menu_items)/sizeof(menu_items[0])) - 1) == current_item_selected){
       current_item_selected = 0;
      } else {
       current_item_selected++;
      }
    }
    void prev(){
      if (current_item_selected == 0){
        current_item_selected = ((sizeof(menu_items)/sizeof(menu_items[0])) - 1);
      } else {
        current_item_selected--;
      }
    }
    String get(){
      return menu_items[current_item_selected].item;
    }
    void run(){
      (*menu_items[current_item_selected].ptrFunc)();
    }
  private:
    int current_item_selected;
    MENULIST menu_items[];
};
/*   MENU FUNCTIONS   */
void ItemOneFunction(){
  Serial.println("One");
}
void ItemTwoFunction(){
  Serial.println("Two");
}
/* ^^ MENU FUNCTIONS ^^ */



EDIT:
I may have to move this question to a new post, this is no longer related to mixed arrays and function pointers, should I?

PaulS

Code: [Select]
    MENULIST menu_items[];
How many items can this array hold? Zip. Zilch. Nada. None. Take your pick.

Code: [Select]
      menu_items = items;
Can you find one case where you've seen someone (successfully) assigned one array to another? You can't, because C doesn't support that. You could use a for loop to copy the elements from one array to another. But, there are two problems with that. First, you have no idea how many elements are in the input array. Second, you have room for exactly 0 elements in the destination array.

myownway

I'm a newbie with C, I'm so sorry to bother you with these noob mistakes.

So, are you telling me that what I want to do is not possible with C?


Go Up