For your first part: The difference is the first word[] array will print correctly through cout<<, and the other one is undefined behavior when printed, because it is not properly null-terminated.
Note that your first excerpt is equivalent to char word[] = "Hello";, which is properly null-terminated implicitly.
c-strings rely on the null termination to know where to stop. The program might still work without it, but that's just because the next character in memory just so happened to be a 0. This could crash your program (or worse, it won't).
if cout << text;
and cout << text.c_str();
have any difference
Yes, check this out:
1 2 3 4 5 6 7 8 9 10 11 12
#include <iostream>
#include <string>
int main()
{
usingnamespace std;
string text = "hello world";
text[6] = '\0'; // replace the 'w' with a null character
cout << text << '\n';
cout << text.c_str() << '\n';
}
std::strings lengths are stored separately, and do not need to be null-terminated. The c_str() will be properly null-terminated either way.