Reputation: 63
I initialized a C++ string with a string literal and replaced a char with NULL.
When printed with cout <<
the full string is printed and the NULL char prints as blank.
When printed as c_str
the string print stop at the NULL char as expected.
I'm a little confused. Does the action came from cout
? or string
?
int main(){
std::string a("ab0cd");
a[2] = '\0'; // '\0' is null char
std::cout << a << std::endl; // abcd
std::cout << a.c_str() << std::endl; // ab
}
Test it online.
I'm not sure whether the environment is related, anyway, I work with VSCode in Windows 10
Upvotes: 4
Views: 2424
Reputation: 11360
First you can narrow down your program to the following:
#include <iostream>
#include <string>
int main(){
std::string a("ab0cd");
a[2] = '\0'; // replace '0' with '\0' (same result as NULL, just cleaner)
std::cout << a << "->" << a.c_str();
}
This prints
abcd->ab
That's because the length of a std::string
is known. So it will print all of it's characters and not stop when encountering the null-character. The null-character '\0'
(which is equivalent to the value of NULL
[both have a value of 0, with different types]), is not printable, so you see only 4 characters. (But this depends on the terminal you use, some might print a placeholder instead)
A const char*
represents (usually) a null-terminated string. So when printing a const char*
it's length is not known and characters are printed until a null-character is encountered.
Upvotes: 5
Reputation:
string end limit (boundary) is not 0 (NULL) like simple char* but its size keep internally in its member data as it's actually user-defined type (an instantiated object) as opposed to primitive type, so
int main(){
string a("abc0d");
a[3] = 0; // '\0' is null char
a.resize(2);
std::cout << a << std::endl; // ab
std::cout << a.c_str() << std::endl; // ab
}
i'm sorry change your code to be more comfortable, watch as it results in
ab
ab
good learning: http://www.cplusplus.com/reference/string/string/find/index.html
Upvotes: 0
Reputation: 8359
Contrary to what you seem to think, C++ string are not null terminated.
The difference in behavior came from the <<
operator overloads.
This code:
cout << a.c_str(); // a.c_str() is char*
As explained here, use the <<
overloads that came with cout
, it print a char array C style and stop at the first null char. (the char array should be null terminated).
This code:
cout << a; // a is string
As explained here, use the <<
overloads that came with string
, it print a string object that internally known is length and accept null char.
Upvotes: 2