Reputation: 1090
#include<iostream>
#include<string>
using namespace std;
int main(){
string a;
a="this is string\0 this part not assigned why?";
cout<<a;
cout<<"sth";
cout<<endl;
a.push_back('\0');
cout<<a;
cout<<"sth";
}
Q: Why is the content after the \0
not assigned to the string a
?
Upvotes: 0
Views: 96
Reputation: 15491
You are assigning the value of a null terminated string literal to your std::string
variable. The string literal gets cut off at the null terminator \0
because it is of type const char[]
so the resulting string is:
this is string
If you want to keep the entire string then use the string's operator""s, add the s
suffix and compile with the C++14 support enabled. This will form a string literal of type std::string
:
std::string a = "this is string\0 this part not assigned why?"s;
std::cout << a;
The result is now:
this is string this part not assigned why?
The reference states (emphasis mine):
Initialization with a string literal that contains embedded '\0' characters uses the overload (5), which stops at the first null character. This can be avoided by specifying a different constructor or by using operator""s:
Upvotes: 3
Reputation: 5321
To use the entire string literal as the value of the string variable, you can pass in the length of the string literal. That can include the explicit and implicit '\0'
characters as well.
#include <cstddef>
#include <string>
#include <iostream>
using std::string;
using std::cout;
using std::endl;
template<typename T, std::size_t size>
std::size_t GetArrayLength(T(&)[size]){
return size;
}
int main() {
string a;
char const s[] = "this is a string\0 this part not assigned why?";
a = string{s, GetArrayLength(s)};
cout << a << endl;
for (auto c : a) {
if (c >= ' ' && c <= '~')
cout << c;
else
cout << "<" << (int)c << ">";
}
cout << endl;
return EXIT_SUCCESS;
}
Upvotes: 1
Reputation: 830
C and C++(for c-style strings) mark end of string with '\0'
character. Suppose "hi"
as an string. It contains three characters i.e h
, i
and \0
actually.
Upvotes: 1
Reputation: 234635
The expression a = "this is string\0 this part not assigned why?"
is using the std::string
assignment operator takes a const char*
as an argument. (The const char[]
literal decays to a const char*
.)
That operator is defined to stop reading when it encounters the first NUL-terminator. In your case, that will be the explicit \0
in the input literal, not the implicit one at the end.
Upvotes: 2
Reputation: 6395
’\0’
denotes the end of a string, so any characters coming behind it are not considered a part of the string by most functions, including cout
.
Consider that these functions need to have a way to know how far to go in the string (otherwise they would continue through all the memory), and th ’\0’
is the agreed-upon end-marker.
If you want a newline, use ’\n’
.
Upvotes: -1
Reputation: 37217
In C++, C-style strings are terminated by a zero character.
This code:
a = "this is string\0 this part not assigned why?";
You're calling the function string& string::operator =(const char*)
, which determines the end of string by an occurrence of a zero. Of course it thinks the string is over when it sees \0
.
FYR, if you want a newline character, use \n
instead of a zero character.
Upvotes: 2