Null character in arrays

https://www.arduino.cc/reference/en/language/variables/data-types/string/
States:

the compiler will size the array to fit the string constant and a terminating null character

If I do

 char test1[] = {1,2,3,4,5, '\0'};
   Serial.print("sizeof(test1)");Serial.print("=");
    Serial.println(sizeof(test1));

The result = 6

and if I do

char test1[] = {1,2,3,4,5};
   Serial.print("sizeof(test1)");Serial.print("=");
    Serial.println(sizeof(test1));

Result = 5
Why?
Isn't the compiler supposed to add an extra "null" character at the end automatically.

Also, can someone please give an example of why the NULL character is necessary?
Arrays are usually addressed to the specific array number anyway. In what situation would I have "random data" come in?
Thanks

char test1[] = {1,2,3,4,5};

initializes the elements explicitly, thus it would be stupid to “automatically” add a terminating null. Arrays are used for all kinds of purposes, it should be left up to the programmer what they want to do with them.

However when you specify a string as a string constant, for example

char test1[] = "chars";

it’s obvious that a character string is desired, so test1 will have a null appended and be 6 bytes long.

A null character is required, because some method is needed to signal the end of a string. How else would you like to do it? :slight_smile:

When was the last time you typed into a computer? That would be “random data coming in”… :slight_smile:

It will add a NULL to an ASCII c-string defined as, say:

char
    szFoo[] = "Test String";

void setup() 
{  
    Serial.begin( 115200 ); 
    Serial.print( "sizeof( szFoo ) = " ); Serial.println( sizeof( szFoo ) );
    Serial.print( "strlen( szFoo ) = " ); Serial.println( strlen( szFoo ) );
.
.
.

Output:

sizeof( szFoo ) = 12
strlen( szFoo ) = 11

But strlen() won't include that null when reporting the length of the array.

aarg:

char test1[] = {1,2,3,4,5};

initializes the elements explicitly, thus it would be stupid to "automatically" add a terminating null. Arrays are used for all kinds of purposes, it should be left up to the programmer what they want to do with them.

However when you specify a string as a string constant, for example

char test1[] = "chars";

it's obvious that a character string is desired, so test1 will have a null appended and be 6 bytes long.

A null character is required, because some method is needed to signal the end of a string. How else would you like to do it? :slight_smile:

When was the last time you typed into a computer? That would be "random data coming in"... :slight_smile:

Got it. Thanks. Do you have an example of where NOT including a NULL char will cause erroneous results?

ShmuelGlick:
Got it. Thanks. Do you have an example of where NOT including a NULL char will cause erroneous results?

That is left as an exercise to the reader.

aarg:
That is left as an exercise to the reader.

If you say so. Thanks

ShmuelGlick:
Got it. Thanks. Do you have an example of where NOT including a NULL char will cause erroneous results?

struct
{
  char hello[6];
  char world[7];
} HW ={{'H', 'e', 'l', 'l','o', ' '}, {"world."}};
void setup()
{
  Serial.begin(115200);
  Serial.println(HW.hello);
  Serial.println(HW.world);
}
void loop() {}

Output:

Hello world.
world.