Use of null character in strings (C++)

一曲冷凌霜 提交于 2019-12-03 12:23:35
sean

From what I remember, the first two are in essence just an array and the way a string is printed is to continue to print until a \0 is encounterd. Thus in the first two examples you start at the point offset of the 6th character in the string, but in your case you are printing out the 6th character which is t.

What happens with the string class is that it makes a copy of the string into it's own internal buffer and does so by copying the string from the start of the array up to the first \0 it finds. Thus the t is not stored because it comes after the first \0.

Because the std::string constructor that takes a const char* treats its argument as a C-style string. It simply copies from it until it hits a null-terminator, then stops copying.

So your last example is actually invoking undefined behaviour; word2[6] goes past the end of the string.

You are constructing a string from a char* (or something that decayed to that). This means that the convention for C-strings apply. That is they are '\0' terminated. That's why word2 only contains "hello".

Gerasimos R

The problem is that you are not printing strings at all - you are printing single characters.

char word [] = "hello\0there";//Array of char...
cout << word[6] << endl;      //So word[6] is the char't' (NOT a string)

string word2 = "hello\0there"; //std::string...
cout << word2[6] << endl;      //so word2[6] is the char 't' (NOT a string as well)

So, you are invoking the "char" overloads, not the "char*" or "string" overloads at all, and the NULL chars have nothing to do with it at all : You are just printing the 6th character of word, and the 6th character of word2.

If I am reading your intent correctly, your test should read:

cout << &(word[6]) (char*, should print "there")
cout << &(word2[6]) (char* as well, undefined behaviour pre-C++11)

In C++11 and later this will also print "there" And be well defined

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!