What does '\0' mean?

后端 未结 6 1691
梦如初夏
梦如初夏 2020-12-29 05:55

I can\'t understand what the \'\\0\' in the two different place mean in the following code:

string x = \"hhhdef\\n\";
cout << x << endl;
x[3]=\'\         


        
6条回答
  •  感情败类
    2020-12-29 06:32

    You're representing strings in two different ways here, which is why the behaviour differs.

    The second one is easier to explain; it's a C-style raw char array. In a C-style string, '\0' denotes the null terminator; it's used to mark the end of the string. So any functions that process/display strings will stop as soon as they hit it (which is why your last string is truncated).

    The first example is creating a fully-formed C++ std::string object. These don't assign any special meaning to '\0' (they don't have null terminators).

提交回复
热议问题