Thanks for your answers.
BulldogLowell:
I'm guessing that each element of the array won't exceed say three or even four bytes. So construct the array to accommodate the largest:
char myArray[10][[3]={
{NULL, ':', ')'},
{':', '-', ')'},
...
};
Delta_G:
I would construct them as strings to make printing a little more friendly and put the null on the end.
char myArray[10][5] = {
{ ':' , ')' , 0} ,
{ ':' , '-' , ')' , 0},
...
Well, I lied! The library won't store smileys specifically, the strings given might be longer than that. I wanted to simplify my question, sorry about that. So I can't use this method, as I don't want to waste too much space allocating this array.
Delta_G:
The thing about not knowing the number ahead of time is that there is a very limited amount of RAM to work with. Using malloc every time they add a new one isn't really practical in this environment. You could have the user call a method (or possibly put it in the class constructor) in the beginning that states how many they will create and malloc the array ONCE and only once at the beginning of the program. That's probably the safest option.
I know malloc is bad in an embedded environment, but the user might call addSmiley later in the program, based on events for example. So the size of the array won't be fixed anyway.
Memory fragmentation should not be a problem, because the sketch runs on an ESP8266 which has sufficient RAM for what I want to do, and addSmiley will probably not be called more than a dozen of times.
PaulS:
Another option is a linked list. Adding a new node to the list is relatively simple. It involves calling malloc() with the size of the node, and then storing the pointer to the new node in the last node created.
The advantage is that you use the minimum amount of storage for the known data. The disadvantage comes from needing to understand pointers and how to create and add to a linked list, plus the overhead of the pointer to the next node, for each node.
I like this one! So something like:
struct Smiley {
String text;
Smiley *next;
};
int size = 0;
Smiley *top;
top = nullptr;
void add(String text){
Smiley *smiley = new Smiley();
smiley->text = text;
smiley->next = top;
top = smiley;
size++;
}
But then, how to iterate in a for loop without popping elements?