Home > OS >  Syntax for declaring a pointer to an array of struct as a parameter
Syntax for declaring a pointer to an array of struct as a parameter

Time:03-26

I am building an Arduino project in C . I have an OLED display that I want to use to present a number of menus to the user. The behaviour of these menus will be the same but the number and detail of the options is variable. So I have created a base class (RotaryMenu) that will define the common methods and then a number of child classes that will define their specific options.

The base class methods will need to access the detail of the options, so I am calling the base class constructor from the child class with a pointer to an array containing the data that is specific to the child class.

I think that this is the correct approach for an embedded application where memory is valuable.

I am struggling to the correct syntax for passing the pointer to the array. I have tried various things but the C compiler seems determined to thwart my intentions

Here is the code;

struct MenuOption {
  char text[21];
  unsigned value;
 };

//Menu base class - not directly useable
//You must creat a child class that populates
//the menu optionList etc.

class RotaryMenu {
  private:
    MenuOption optionList[];
    char* title;
    size_t numOptions;
  public:
    //Constructor populates optionList
    RotaryMenu( char * _title, MenuOption *options, size_t num ) {
      optionList = options;
      title = _title;
      numOptions = num;
    }
  protected:
    void show(unsigned first);
};

//Example child class
//Calibration menu -
class CalibrationMenu : public virtual RotaryMenu {

  public:
    CalibrationMenu() : RotaryMenu( title, optionList, numOptions );
    const char* title {"Calibration Menu"};
    const size_t numOptions {4};
    const MenuOption optionList[4] = {
      {"Boat compass hdg.",1},
      {"Reload saved offsets",2},
      {"Manual calibration",3},
      {"Cancel and return",0}
    };
 } calibrationMenu;


CodePudding user response:

Change the optionList member variable to be of type MenuOption const* instead of an invalid empty array, adjust the constness of your other various parameters and members, and add a function body to the CalibrationMenu constructor definition.

Then worry about construction order, because base classes are constructed before member variables.

CodePudding user response:

const MenuOption optionList[4] ...

This is an array of const objects.

RotaryMenu( title, optionList, numOptions );

When it gets passed as a parameter, the array decays to a const MenuOption *, accordingly, a pointer to const objects.

RotaryMenu( char * _title, MenuOption *options, size_t num ) {

But the parameter is declared as a MenuOption *, instead of a const MenuOption *. C does not allow a pointer to const objects to get converted to a pointer to non-const objects. This has nothing to do with arrays. The same thing will happen if plain pointers were used from the beginning.

MenuOption optionList[];

This class member declaration is also confusing, if not plain wrong. This should also be a pointer, and for the same reasons as above this should be a const MenuOption *optionList.

Note that this entire arrangement results in a base class having a pointer to a member of its (virtual) sub-class. This is not completely wrong, per se, but will result in surprising behavior if these objects get copied or moved.

If your intent was not that, but for the subclass to actually own and contain an array of its own, then the whole thing needs to be done in a completely different manner.

  • Related