How to properly store and use char array

hello there,

I'm on a project where pushing a button prints a ticket with a silly joke or whatever text you've stored in the device.

I'm currently struggling with char array...

What I need to do :

  1. Store efficiently multiple lines text
  2. be able to randomly choose a text from the list
  3. send line after line to the printer

How I tried to do :

I created a struct to store the texts with two variables:

  1. An int representing the amount of lines
  2. an array of const char* with the text

i succeeded to get the int value, but can't get the lines values.... each time I try to print a line, it's empty or it crashes the ESP32...

Here is the code involved.

Thank's for helping !

#include <Arduino.h>

#ifndef BLAGUES_H
#define BLAGUES_H

typedef struct{
   int nbLines;
   const char *jokeLines[];
}Joke;

const int numberOfJokes = 8;


Joke joke1 = {
    2,
    {
        "- Quelle est la difference entre les bieres et les chasseurs ?",
        "- Les bieres, on arrive a en faire des sans alcool"
    }
};
Joke joke2 = {
    2,
    {
        "- Que dit une biere qui tombe dans l'eau ?",
        "- Je sais panache."
    }
};
Joke joke3 = {
    1,
    {
        "- Qui a mis des legumes dans le bac a biere ?"
    }
};
Joke joke4 = {
    1,
    {
        "- C'est l'histoire d'un aveugle qui rentre dans un bar, puis dans une chaise, puis dans une table, ..."
    }
};
Joke joke5 = {
    2,
    {
        "- Pourquoi les anges sont sourds ?",
        "- Parce que Jesus crie !"
    }
};
Joke joke6 = {
    2,
    {
        "- Pourquoi Napoleon n'a jamais voulu s'acheter de maison ?",
        "- Parce qu'il avait deja un bon appart !"
    }
};
Joke joke7 = {
    2,
    {
        "- Pourquoi roule-t-on doucement dans le Nord ?",
        "- Parce que les voitures n'arretent pas de caler !"
    }
};
Joke joke8 = {
    2,
    {
        "- Comment appelle-t-on un mort qui coupe du fromage ?",
        "- Un Fend-Tome !"
    }
};

Joke jokesList[numberOfJokes]
{
    joke1,
    joke2,
    joke3,
    joke4,
    joke5,
    joke6,
    joke7,
    joke8,    
};

#endif

And the function I'm using for tests is :

void test()
{
  Serial.println("SILLY JOKES");

  for ( int i =0; i < numberOfJokes; i++ )
  {
    Serial.print("Joke Index : ");
    Serial.println(i);
    int j = jokesList[i].nbLines;
    Serial.print("nb lines in joke : ");
    Serial.println(j);

    for ( int k = 0; k < j; k++ )
    {
      Serial.println(jokesList[i].jokeLines[k]);
    }
  }

}

A 2D character array is one possibility, a second would be a 1D array of pointers to character arrays. Keep in mind that C-strings are zero terminated character arrays.

Lots of tutorials on line! One randomly chosen example: Array of Strings in C - GeeksforGeeks

A 2D character array ?
Even if all my texts don't have the same amount of lines ?
And if each line has a different number of character ?

I tried the 1D array of pointers to char array, but I need to know the amout of line for each text, that is why I made the struct. But may be there is a smarter solution ?

No. See the example tutorial I linked.

You need to specify how many elements are in the jokeLines array, which presumably would be the largest number of lines of text you intend to store.

typedef struct{
   int nbLines;
   const char *jokeLines[2];
}Joke;

Alternatively, you can just store a single large line of text for each joke, with the carriage return and linefeed embedded in the text. Then you only need an array of char* to the jokes, and would not need to know how many lines the joke was composed of.

First, it looks like you're putting variable definitions in a .h file. Never do that!!! If that .h gets #inlucde(d) in multiple .cpp or .ino files, you'll end up with linker errors.

Second, why do you need to know the number of lines in each joke anyway?

const char *jokes[] {
  "- Quelle est la difference entre les bieres et les chasseurs ?\n- Les bieres, on arrive a en faire des sans alcool",
  "- Que dit une biere qui tombe dans l'eau ?\n- Je sais panache.",
  "- Qui a mis des legumes dans le bac a biere ?",
  "- C'est l'histoire d'un aveugle qui rentre dans un bar, puis dans une chaise, puis dans une table, ...",
  "- Pourquoi les anges sont sourds ?\n- Parce que Jesus crie !",
  "- Pourquoi Napoleon n'a jamais voulu s'acheter de maison ?\n- Parce qu'il avait deja un bon appart !",
  "- Pourquoi roule-t-on doucement dans le Nord ?\n- Parce que les voitures n'arretent pas de caler !",
  "- Comment appelle-t-on un mort qui coupe du fromage ?\n- Un Fend-Tome !"
};



void setup() {
  Serial.begin(115200);
  delay(1000);
  for (auto j : jokes) {
    Serial.println(j);
    Serial.println();
  }
}

void loop() {
}

Serial Port Output:

- Quelle est la difference entre les bieres et les chasseurs ?
- Les bieres, on arrive a en faire des sans alcool

- Que dit une biere qui tombe dans l'eau ?
- Je sais panache.

- Qui a mis des legumes dans le bac a biere ?

- C'est l'histoire d'un aveugle qui rentre dans un bar, puis dans une chaise, puis dans une table, ...

- Pourquoi les anges sont sourds ?
- Parce que Jesus crie !

- Pourquoi Napoleon n'a jamais voulu s'acheter de maison ?
- Parce qu'il avait deja un bon appart !

- Pourquoi roule-t-on doucement dans le Nord ?
- Parce que les voitures n'arretent pas de caler !

- Comment appelle-t-on un mort qui coupe du fromage ?
- Un Fend-Tome !

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.