char *pt = "hello";
std::string str = "hello";
Does str
also end with '/0'
(is null terminated)?
char *pt = "hello";
std::string str = "hello";
Does str
also end with '/0'
(is null terminated)?
It is implementation defined whether or not std::string
is null-terminated.
The actual contents of str
after its definition:
std::string str = "hello";
are characters 'h'
, 'e'
, 'l'
, 'l'
, 'o'
and its size
is equal only to 5 characters.
The buffer manged by str
MAY be null terminated, but not necessarily.
str.c_str()
will provide you a const char*
, which pointed to a null-terminated, contiguous buffer (like your "hello"
string literal was).
But be aware of using &str[0]
because it's not guaranteed that this points to a contiguous buffer, neither that this buffer is null-terminated.