Pb when using String

Hello.
I try to use a table of 10 Strings and I'm facing a pb with the first value of the table :

Here is the code :slight_smile:

String table_of_the_messages[10] = {"123 456 7890 123 456", "123456", "123"," ", " ","1","2","12345678","1234     5678"};

void setup()
{
  Serial.begin(115200);
  Serial.println();
  create_message(); // routine qui envoie le email
}

void loop()
{
  // dans cet exemple, le email est envoyé une seule fois, au démarrage du programme
}

void create_message()
{
  char content_message[100];
  // construction du corps du message (inclusion d'un nombre aléatoire)
  
  for (int n=0; n< 10; n++)
  {    
    sprintf(content_message, "ALERTE - %s", table_of_the_messages[n]);
    Serial.println(content_message);
  }
}

Here is the result : >>> Why the first value is with error ?

19:03:58.670 -> ALERTE - (��?
19:03:58.670 -> ALERTE - 123456
19:03:58.670 -> ALERTE - 123
19:03:58.670 -> ALERTE -  
19:03:58.670 -> ALERTE -  
19:03:58.670 -> ALERTE - 1
19:03:58.670 -> ALERTE - 2
19:03:58.670 -> ALERTE - 12345678
19:03:58.670 -> ALERTE - 1234     5678
19:03:58.670 -> ALERTE -

As your topic is not a suggestion for the Arduino project it has been moved to a more relevant forum category

When I run your sketch, I see the following:

Monitor port settings:
baudrate=115200
Connected to /dev/ttyUSB0! Press CTRL-C to exit.
&&�@CB�
ALERTE - �
ALERTE - �
ALERTE - �
ALERTE - �
ALERTE - �
ALERTE - �
ALERTE - �
ALERTE - �
ALERTE - �	
ALERTE - �

Turning the warning level all the way confirms what I already knew:

arduino-cli compile -b arduino:avr:nano:cpu=atmega328 --warnings all --output-dir /home/me/tmp --no-color (in directory: /home/me/Documents/sketchbook/Nano/test)
/home/me/Documents/sketchbook/Nano/test/test.ino: In function 'void create_message()':
/home/me/Documents/sketchbook/Nano/test/test.ino:22:65: warning: format '%s' expects argument of type 'char*', but argument 3 has type 'String*' [-Wformat=]
 sprintf(content_message, "ALERTE - %s", table_of_the_messages[n]);
                                                                 ^
Sketch uses 4564 bytes (14%) of program storage space. Maximum is 30720 bytes.
Global variables use 326 bytes (15%) of dynamic memory, leaving 1722 bytes for local variables. Maximum is 2048 bytes.
Used platform Version Path
arduino:avr   1.8.3   /home/me/.arduino15/packages/arduino/hardware/avr/1.8.3
Compilation finished successfully.

The %s in the sprintf is expecting a pointer to a nul terminated character array, not a String.

Change your array to

char *table_of_the_messages[10] = {...

Or same some ram and processing time

  for (int n = 0; n < 10; n++)
  {
    Serial.print("ALERTE - ");
    Serial.println(table_of_the_messages[n]);
  }

Your attempt on code tags failed; I've fixed it.

It's not literally <CODE/> :wink: <CODE/> is the text of the button. Code tags are three backticks (```) on their own line at the beginning and the end of the block of code. I have also taken the liberty to put your serial monitor output in code tags.

You can specify what you want to display (cpp, java, text, ...); I've used that. ```cpp at start of the code and ```text at the start of the serial monitor output (it prevents unwanted colouring). If you edit your post you can see what was done.

In my experience the easiest way to tidy up the code and add the code tags is as follows

Start by tidying up your code by using Tools/Auto Format in the IDE to make it easier to read. Then use Edit/Copy for Forum and paste what was copied in a new reply. Code tags will have been added to the code to make it easy to read in the forum thus making it easier to provide help.

What is "pb"????

@christophe782
My crystal ball suggested pb means "problem" . Right?

plebnum

Of course, we may be being lead astray

1 Like

Hi. Yes. Thanks fro your support. I found a solution. Best regards.

The '%s' wants a pointer to a char array, not a String object, which you can get if you call the .c_str() method on your String

    sprintf(content_message, "ALERTE - %s", table_of_the_messages[n].c_str());

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