Append String to Char Array

Hai,

I would like to append a String object to an array of characters.
My code looks something like this:

char *val[] = {};

void setup(){
   Serial.begin(9600);
   String text1 = "123456";
   String text2 = "abcdef";
   text1.toCharArray(val[0], 0);
   Serial.println(val[0]);
   text2.toCharArray(val[1], 1);
   Serial.println(val[1]);
}

void loop(){}

can anyone help me? sorry, I'm so stupid about this😢

what do you think that is ? how many bytes of memory will be allocated and what's the variable's type?

if you use the String class, stick to it

void setup(){
   Serial.begin(9600);
   String text1 = "123456";
   String text2 = "abcdef";
   String text3 = text1 + text2;
   Serial.println(text3);
}

void loop(){}

if you want the underlying cString (the null terminated char array) then it's text3.c_str()

Sorry, I don't really understand arrays. i still newbie😢
can you explain it for me?

long story short, it's not what you want. There is 0 element in the array of pointers you defined and no memory to store content.

I was editing my post as you typed to add more info, read again my answer

I want to create a list of strings, which can be called.

for example:

char *val[2] = {"12345", "abcde"};

void setup(){
   Serial.begin(9600);
   Serial.println(val[0]);
}

that's almost good, just missing const (and no need to give the count, the compiler will find out)

const char *val[] = {"12345", "abcde"};

void setup(){
   Serial.begin(9600);
   Serial.println(val[0]);
}

void loop() {}

ohww okey. but, how to append another strings to array?

you need to make sure there is enough memory pre-allocated

if you define the array as a 2D array then you'll have storage

char messageList[10][20]; // 10 messages of max 19 characters + 1 for the mandatory trailing null char

this reserves 10 x 20 = 200 bytes of memory where you can store your strings. So careful on how much you allocate depending on your Arduino's SRAM

so you could do something like this

const byte maxCount = 5;
const byte maxStringLength = 20;

char messageList[maxCount][maxStringLength] = {"str0", "str1", "str2"};

void setup() {
  Serial.begin(115200);
  // print all the strings (some are empty)
  for (byte i = 0; i < maxCount; i++) Serial.println(messageList[i]);

  // you can use strlcat to concatenate without overflow
  strlcat(messageList[0], " and more", maxStringLength);

  // you can use strlcpy() to initialize a string without overflow
  strlcpy(messageList[3], "some new text 3 ", maxStringLength);

  // print all the strings (some are empty)
  for (byte i = 0; i < maxCount; i++) Serial.println(messageList[i]);
}

void loop() {}

➜ in wokwi sketch.ino - Wokwi Arduino and ESP32 Simulator

there are many functions working on cStrings, you can find them in stdlib.h and string.h

wooww🤩 thank u so much

how about this code?

char *val[] = {};

void setup(){
  Serial.begin(9600);
  String msg1 = "12345";
  String msg2 = "abcde";
  val[0] = msg1.c_str();
  Serial.println(val[0]);
  delay(500);
  val[1] = msg2.c_str();
  Serial.println(val[1]);
}

void loop(){}

is there a problem with the code?

Yes there is

can you see the differencies?
you have to

explicitly specify

how much memory you want to use
which is done here

char messageList[maxCount][maxStringLength]

with your version

the compiler has

no idea

how many strings you want to store and how long each string is

if you don't want to specify how long each string is use variable-type String
until your code crashes because careless re-assigning values to Strings eats up all memory over time

ok, thanks a lot for your advice🤩

yes there is no size given to the val array.

so you can't store anything there

if you want to know, just do a Serial.println(sizeof val); after the Serial.begin() and you'll see 0 bytes were allocated

array's don't grow dynamically, you have to provide the max size

side note: c_str() returns a const char * so you would need to define the array as

const char *val[2];

to be able to ensure type compatibility

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