Why are strings in C++ usually terminated with '\0'?

后端 未结 5 1072
心在旅途
心在旅途 2020-11-27 18:43

In many code samples, people usually use \'\\0\' after creating a new char array like this:

string s = \"JustAString\";
char* array = new char[         


        
5条回答
  •  野性不改
    2020-11-27 19:14

    '\0' is the null termination character. If your character array didn't have it and you tried to do a strcpy you would have a buffer overflow. Many functions rely on it to know when they need to stop reading or writing memory.

提交回复
热议问题