Use of null character in strings (C++)

时光怂恿深爱的人放手 提交于 2019-12-21 04:06:35

问题


I am brushing up on my C++ and stumbled across a curious behavior in regards to strings, character arrays, and the null character ('\0'). The following code:

#include <iostream>
using namespace std;

int main() {
    cout << "hello\0there"[6] << endl;

    char word [] = "hello\0there";
    cout << word[6] << endl;

    string word2 = "hello\0there";
    cout << word2[6] << endl;

    return 0;
}

produces the output:

> t
> t
>

What is going on behind the scenes? Why does the string literal and the declared char array store the 't' at index 6 (after the internal '\0'), but the declared string does not?


回答1:


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.




回答2:


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.




回答3:


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".




回答4:


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



来源:https://stackoverflow.com/questions/11582365/use-of-null-character-in-strings-c

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