How to make a Char* array with text and int numbers

Hi Forum.

I am making some code for sending to an LCD screen, so it is scrollable. The code reads a char* array like this:

char* Menu[] = {
  "Sep Break","Continue Sep","Terminate Sep","Skip 'm'","Skip 's'","skip 'gT'","Skip 'gD'"," ",'\0'};

This works perfect, but is it possible for me to change some of the names in this array?

Say i would like to do this sudo-code:

String text = "SensorValue =";
Int Number = 12;

Menu[2] = text + Number;

Essentially substituting "Terminate Sep" from the above code with "SensorValue =12".

I have tried googling and read a lot of posts, but none seem to work or be able to do the above mentioned thing. Hope that you can help. Thanks in advance :slight_smile:

Best regards

It is not a good idea to use the String (capital S) class on an Arduino as it can cause memory corruption in the small memory on an Arduino. This can happen after the program has been running perfectly for some time. Just use cstrings - char arrays terminated with '\0' (NULL).

You can copy data into your array using the strcpy() function - but be very careful not to exceed the original size of the array.


How about adding numbers (as needed) in the display process:

char* Menu[] = {
  "Sep Break","Continue Sep","SensorValue = ",
  "Skip 'm'","Skip 's'","skip 'gT'","Skip 'gD'",
  " ",'\0'};

int Number = 12;

displayMenuEntry(int index)
  switch (index)
    case 2: 
    // "SensorValue = "

I would

  • initialize the buffer to hold the resulting string from PROGMEM
  • append an ASCII representation (base 10) of the number to the buffer
  • replace the pointer in the Menu array with a pointer of the result buffer
const char* Menu[] = {
  "Sep Break", "Continue Sep", "Terminate Sep", "Skip 'm'", "Skip 's'", "skip 'gT'", "Skip 'gD'", " ", '\0'
char sensorValue[21];
int number = 12;

void setup() {
  strcpy_P(sensorValue, PSTR("SensorValue = "));
  itoa(number, sensorValue + strlen(sensorValue), 10);
  Menu[2] = sensorValue;

void loop() {}
SensorValue = 12