For some reason, multiple null ('\0') characters stored in a std::string variable does not reflect when calling length method.
std::string str = "\0\0\0";
std::cout << str.length(); // prints 0
but using a different way of building the string:
std::string var = std::string(3,'\0');
std::cout << var.length(); // prints 3
Why is this so?
C strings are null-terminated and C++ strings aren't. In your fist example, you are implicitly casting a `char*`-type string literal to a `std::string`, and, because in a C string, '\0' is considered end-of-string, it won't read any further.
But you can still put \0 in a C++ string if you want to, which is what you are doing in the second example.
You mean string literals are actually char*, and char*, or string literals in itself, does not store null characters?
... this is just weird, but looks like you are right. It won't even read "\0something". As long as it meets \0, it cuts off. I'd figure that C++ wouldn't interfere with my explicit bidding, but I guess std::string(3, '\0') will work for me.
String literals are char* yeah, but a char* does in fact store the '\0'.
The problem is that a char* has no concept of "length" and so you either have to keep track of the length yourself (std::string), or you just keep on reading until you encounter a special value in memory (\0 in C "strings"). When converting between the two you have to be careful.
One cool thing you can do with C strings is that you can store many strings in a single char*, by inserting \0s in between. That's what the C function `strtok` does for example!
The first line implicit construct a std::string by detecting length of "\0\0\0", and copy the string into it. The length detection is something like strlen("\0\0\0"), which is same as strlen("\0"), so it construct a zero length std:: string.
While the 2nd std:: string is constructed with explicit length.
Fermi If you want to store string literal with null characters in string, you may use:
string str("\0something", 9);
It is 4th constructor: https://en.cppreference.com/w/cpp/string/basic_string/basic_string
Check it in my code.
~ swim ~ Raw string cannot contain null character. Your escape sequence "\0" inside raw string are in fact two characters: '\' and '0'. Check it in my code: