I'm trying to define an instance of a struct, and am having particular trouble with setting this one variable. It's an array of char arrays.
Here is my struct in my header file...
struct widget_t {
char *name;
uint8_t numberOfNicknames;
char *nicknames[];
};
And here's me attempting to set up the instance of the widget_t
struct...
widget_t SomeWidget;
void setUpFunction () {
SomeWidget.name = (char *)"Lawn Mower";
SomeWidget.numberOfNicknames = 2;
SomeWidget.nicknames = {
"Choppie McGrasschopper",
"Really Noisy"
};
}
So, the error is happening when I try to put the nicknames into SomeWidget.nicknames
. I'm not sure if I need to do something funky like I'm doing with name
being a pointer...?
The tricky bit is that the number of nicknames
is variable. So each instance will want to set up a different number of them.
One option would be:
struct widget_t {
char const *name;
uint8_t numberOfNicknames;
char const * const *nicknames;
};
static char const *mower_nicknames[] = { "Choppie", "Bob" };
widget_t SomeWidget = { "Lawn Mower", 2, mower_nicknames };
static char const *bus_nicknames[] = { "Wheels", "Go", "Round" };
widget_t OtherWidget = { "Bus", 3, bus_nicknames };
// no setup function needed