Unwanted Fusion of tow Char Arrays

Hey Guys,
i can´t figure out why the "text" array is combined within the "checkArray" whenever the "checkArray" is used.

void setup() {
  Serial.begin(9600);
}

void loop() {
  char text[] = {'1', '2', '3', '4', '5'};
  const char checkArray[] = {'1', '1', '1', '1', '1'};
  Serial.println(checkArray);
  Serial.println(sizeof(checkArray));
  Serial.println(text);
}

Here is the output I get:

16:28:25.916 -> 1111112345
16:28:25.951 -> 5
16:28:25.951 -> 12345

I hope you guys can help.
Thanks

You are effectively printing C strings. They need a terminating zero, which you're missing, so one of your prints overflows to print the next array too.

Neither array is a string; you should not be using println to print them.

See comments above. A modified sketch:

void setup() {
  Serial.begin(9600);

  char text[] = {'1', '2', '3', '4', '5'};
  const char checkArray[] = {'1', '1', '1', '1', '1','\0'}; // Introduction of terminating null
  Serial.println(checkArray);
  Serial.println(sizeof(checkArray));
  Serial.println(text);
}

void loop() {
}

When you give println() a char*, which is what the array name without brackets is, it expects a null-terminated char array, and will print whatever it finds in memory between the memory address pointed to by the char* to the first memory location that contains a null (0x00).

You can either print each array element individually, or use Serial.write since the data is in ASCII format.

Serial.write(checkArray, sizeof(checkArray));
Serial.write('\n'); \\prints a newline character

Note that your original code prints the text array correctly only because the next location in memory contains 0x00 (or at least there are no printable characters before the first memory location with 0x00). Printing checkArray prints both checkArray and text because the arrays are stored adjacent to each other in memory with no intervening 0x00.

Another modified sketch. :slight_smile:
Double quotes will automatically add the terminating null (and sizeof will report 6):

void setup() {
  Serial.begin(9600);

  char text[] = {"12345"};    // this will automatically add the terminating null
  const char checkArray[] = {"11111"}; 
  Serial.println(checkArray);
  Serial.println(sizeof(checkArray));
  Serial.println(text);
  Serial.println(sizeof(text));
}

void loop() {}