c++arraysc-stringsstring-literalsarray-initialization

Why do we need a null terminator in C++ strings?


I'm new to programming and very new to C++, and I recently came across strings.

Why do we need a null terminator at the end of a character list?

I've read answers like since we might not use all the spaces of an array therefore we need the null terminator for the program to know where the string ends e.g. char[100] = "John" but why can't the program just loop through the array to check how many spaces are filled and hence decide the length?

And if only four characters are filled in the array for the word "John", what are the others spaces filled with?


Solution

  • The other characters in the array char john[100] = "John" would be filled with zeros, which are all null-terminators. In general, when you initialize an array and don't provide enough elements to fill it up, the remaining elements are default-initialized:

    int foo[3] {5};           // this is {5, 0, 0}
    int bar[3] {};            // this is {0, 0, 0}
    
    char john[5] = "John";    // this is {'J', 'o', 'h', 'n', 0}
    char peter[5] = "Peter";  // ERROR, initializer string too long
                              // (one null-terminator is mandatory)
    

    Also see cppreference on Array initialization. To find the length of such a string, we just loop through the characters until we find 0 and exit.

    The motivation behind null-terminating strings in C++ is to ensure compatibility with C-libraries, which use null-terminated strings. Also see What's the rationale for null terminated strings?

    Containers like std::string don't require the string to be null-terminated and can even store a string containing null-characters. This is because they store the size of the string separately. However, the characters of a std::string are often null-terminated anyways so that std::string::c_str() doesn't require a modification of the underlying array.

    C++-only libraries will rarely -if ever- pass C-strings between functions.