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
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 ?
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.
It's not literally <CODE/><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.