search

Home  >  Q&A  >  body text

c++ - 又遇到了'\0' null NULL 的问题了

我之前上 stackoverflow 查证过字符串的结尾是'0'-terminate啊, 如下引用. 但是今天查 string 的, 上面图片里的官方文档的说法又都变成了 null-terminated 了. 希望大家解答下, 谢谢

‘0’ terminate a string, not NULL, the NULL macro isn’t intended for use in terminating strings.----(左边包含了链接)

PHP中文网PHP中文网2814 days ago622

reply all(2)I'll reply

  • 天蓬老师

    天蓬老师2017-04-17 15:25:44

    If it is a string of char*, the library function defaults to '

    #include <string>
    #include <iostream>
    
    int main()
    {
        std::string s = "hello";
        s[1] = 'rrreee';
        std::cout << s << std::endl;
        return 0;
    }

    Output: h llo

    reply
    0
  • 高洛峰

    高洛峰2017-04-17 15:25:44

    The so-called null-terminated in null should refer to the NUL in the ASCII character set. Its encoding is the number 0, which is '' in C language. It is essentially a char.
    In C language, the NULL macro refers to a null pointer, that is, ((void*)0), which is a pointer type.
    The two are different types, essentially different concepts, and have different uses.
    The original post on stackoverflow that you quoted should discuss this issue.

    reply
    0
  • Cancelreply