Assign string variable value through pointer ?

Hi I have some questions explained below. Any help would be greatly appreciated ! Thank you

// I have a pointer to a char array at the beginning
static const char *char_array[] = {"Note", "128", "128", "16", "\n",
                                     "Note", "128", "128", "16", "\n",
                                     "Note", "128", "128", "16", "\n",
                                     "Note", "128", "128", "16", ""};

// At one point program returns a variable x
uint8_t x = 100;

// How do I ?
// 1. Convert variable x into writeable string y (ex: char y[] == "100") ?
// 2. Directly assign the value of element i in char_array (ex:  char_array[i]) 
to the string created above ("100"), not just point it to y ? (So I don't have to keep y in memory)
static const char *char_array[] = {"Note", "128", "128", "16", "\n",
                                   "Note", "128", "128", "16", "\n",
                                   "Note", "128", "128", "16", "\n",
                                   "Note", "128", "128", "16", ""
                                  };
                                  
void setup()
{
  Serial.begin(115200);
  while (!Serial);
  uint8_t x = 100;
  char buffer[10];  //plenty of room
  itoa(x, buffer, 10);
  Serial.println(buffer);
  strcpy(buffer, char_array[0]);
  Serial.println(buffer);
}

void loop()
{
}

There are a few problems,
When the compiler compiles
static const char *char_array
it only allocates 1 string for ALL the “128” values and then just reused the point to that one value in the char_array
so when you update say char_array[1] ALL the 128 values are changed to 100

Second it is very very easy to trample all over memory using low level c-string functions.
This example code uses my SafeString library that protects you from destroying your memory

#include <SafeString.h>
// install from Arduino library manager
// see tutorial at https://www.forward.com.au/pfod/ArduinoProgramming/SafeString/index.html
static const char *char_array[] = {"Note1", "120", "121", "10", "\n",
                                   "Note2", "122", "123", "11", "\n",
                                   "Note3", "124", "125", "12", "\n",
                                   "Note4", "126", "127", "13", ""
                                  };
void printCharArray() {
  size_t arraySize = sizeof(char_array) / sizeof(char*);
  //Serial.print(F("arraySize "));Serial.println(arraySize);
  for (size_t i = 0; i < arraySize; i++) {
    Serial.print(char_array[i]);
    Serial.print(' ');
  }
  Serial.println();
}
void setup() {
  Serial.begin(115200);
  for (int i = 10; i > 0; i--) {
    Serial.print(i); Serial.print(' ');
    delay(500);
  }
  Serial.println();
  SafeString::setOutput(Serial); // turn on SafeString error msg output

  printCharArray();
  uint8_t x = 100;
  //int x = 1000; // error test
  createSafeStringFromCharPtr(element, (char*)char_array[2]);  // or cSFP(element, (char*)char_array[2]) for short
  element.clear(); // clear the existing value
  element.print(x); // write the new value
  printCharArray();
}

void loop() {
}

The output is

Note1 120 121 10 
 Note2 122 123 11 
 Note3 124 125 12 
 Note4 126 127 13  
Note1 120 100 10 
 Note2 122 123 11 
 Note3 124 125 12 
 Note4 126 127 13

If at some later time you change x to an int and try to put 1000 into char_array[2]
the output will be

Note1 120 121 10 
 Note2 122 123 11 
 Note3 124 125 12 
 Note4 126 127 13  
Error: element.print() needs capacity of 4
        Input arg was '1000'
        element cap:3 len:0 ''
Note1 120  10 
 Note2 122 123 11 
 Note3 124 125 12 
 Note4 126 127 13

char_array[2] was cleared but not written with the data that would have caused a buffer overflow.

actually

  uint8_t x = 100;
  cSFP(element, (char*)char_array[2]); // wrap the ptr in a SafeString
  element = x; // write the new value

is easier.
The element = x; does the clear for you and then assigns the new value of x, as a string

Do they have to be strings? This seems like more like something that would work better using structs.

Or a nice class list thing..

-jim lee

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