append to char array

I have a webserver on esp8266 that i want to see debugging information from. i cant store a string in a struct and send it over udp. i want to create a large char array[1500] and store debugging messages in it so i can send it over udp to another node.

i initialize an array ,char array[1500];
then i try to add to the array , array="message";

this don't work. how can i achieve what im trying to do. im not concerned with data being cut off when the array is too big. i need to be able to append messages to the array and detect when its full and clear it. Please help

1 Like

We can’t see your code.

TheMemberFormerlyKnownAsAWOL:
We can't see your code.

char array[1500];
void setup() {
  Serial.begin(9999);
}
void loop() {
  array = "message";
  array = array += "message2";
  if (sizeof(array) >= 1499) {array = "";}
  Serial.println(array);
}

notsolowki:
this don’t work. how can i achieve what im trying to do. im not concerned with data being cut off when the array is too big. i need to be able to append messages to the array and detect when its full and clear it. Please help

Since we can’t see your code when you say this “doesn’t work” does that mean it won’t compile or it just doesn’t do what you think it should when you run the program?

strcpy (array, "message");

You can’t add char arrays.
Use strcat.

if (sizeof(array) >= 1499) FYI, 1500 is always > 1499

TheMemberFormerlyKnownAsAWOL:

strcpy (array, "message");

You can't add char arrays.
Use strcat.

i could use the String class to append to the array i just didn't know if i should and could use toCharArray()

You need to use strXXX functions:

  strcpy(array,"message");
  strcat(array,"message2");

ToddL1962:
Since we can’t see your code when you say this “doesn’t work” does that mean it won’t compile or it just doesn’t do what you think it should when you run the program?

i initialize an array ,char array[1500];
then i try to add to the array , array=“message”;

You CANNOT add char arrays.
Use strcat.

What is this line of code for?

  if (sizeof(array) >= 1499) {array = "";}

sizeof(array) will always be 1500.

TheMemberFormerlyKnownAsAWOL:
You CANNOT add char arrays.
Use strcat.

but i CAN add Strings to a char array. i could overwrite the char array with a large string every time i want to add a message?

however if this will append to the array then that will work,

strcpy(array,"message");
strcat(array,"message2");

how can i prevent overflowing the array and writing outside its memory?

notsolowki:
how can i prevent overflowing the array and writing outside its memory?

Self-discipline.

strncat()

notsolowki:
how can i prevent overflowing the array and writing outside its memory?

Add the size of the string you want to append to strlen(array) and if it is >= sizeof(array) then don't append!

Thanks this should get me started. so when the size of the String >= to max then String =""; atleast hopefully i understand this right.

Please be aware the sizeof (String) is always 6 on an AVR.

TheMemberFormerlyKnownAsAWOL:
Please be aware the sizeof (String) is always 6 on an AVR.

this is how i am seeing what everyones trying to say.

if strlen(array) >= 1499){
array="";
}

I am using an ESP8266

notsolowki:
Thanks this should get me started. so when the size of the String >= to max then String =""; atleast hopefully i understand this right.

First of all a char array is not the same as a String object. Secondly you must check if the size will be greater than the max BEFORE you append otherwise you will corrupt memory!

Here is a simple example:

if (strlen("message2") + strlen(array) >= sizeof(array)) array[0] = '\0';
strcat(array, "message2");

this is how i am seeing what everyones trying to say.

if strlen(array) >= 1499){
array="";
}

You really need to think about how char arrays are used.
If you want to empty a string, then array[]=""; is NOT the way to do it.

array [0] = '\0';

I am using an ESP8266

This is news.