简体   繁体   English

nullptr用于终止C风格的字符串吗?

[英]Is nullptr used to terminate C-style strings?

I am confused by the use of nullptr in an example from A Tour of C++ : 我对A Tour of C ++中的nullptr的使用感到困惑:

int count_x(char* p, char x)
// count the number of occurrences of x in p[]
// p is assumed to point to a zero-terminated array of char (or to nothing)
{
    if (p==nullptr) return 0; 
    int count = 0;
    for (; p!=nullptr; ++p)
        if (*p==x) ++count;
    return count; 
}
// The definition of count_x() assumes that the char* is a C-style string,
// that is, that the pointer points to a zero-terminated array of char.

I understand that count_x should terminate if p is unassigned, and the for loop should terminate when it reaches the end of the C-style string referenced by p. 我知道如果p未被赋值,count_x应该终止,并且for循环应该在它到达p引用的C风格字符串的末尾时终止。

However, when I build a main function to use count_x(), it never seems to terminate correctly: 但是,当我构建一个使用count_x()的main函数时,它似乎永远不会正确终止:

int main () {

    char teststring[] = {'b', 'l', 'a', 'h', '\0'}; 
    cout << "teststring is: " << teststring << endl; 
    cout << "Number of b's is: " << count_x(teststring, 'b') << endl; 

    return 0;  
}

Executing this prints a lot of garbage, and then exits with a segmentation fault. 执行此操作会打印大量垃圾,然后以分段错误退出。 If I replace the for (; p!=nullptr; ++p) in count_x with for (; *p!='\\0'; ++p) , it executes properly. 如果我用for (; *p!='\\0'; ++p) count_xfor (; p!=nullptr; ++p) for (; *p!='\\0'; ++p) ,它会正确执行。 I guess this means that the string is not terminated correctly. 我想这意味着字符串没有正确终止。 If so, how do I terminate a C-style string so that nullptr can be used here? 如果是这样,我如何终止C风格的字符串,以便可以在这里使用nullptr?

Edit: there's been a discussion in the comments that's clarified this situation. 编辑:评论中有一个讨论澄清了这种情况。 I'm using the first print of the book from September 2013, where the above is printed in error. 我正在使用2013年9月出版的第一本书,上面的内容是错误打印的。 The third print of the book from January 2015 (linked in the comments) has the corrected example which uses for (; *p!=0; ++p) instead of for (; p!=nullptr; ++p) . 本书从2015年1月开始的第三版(在评论中链接)有一个更正的例子, for (; *p!=0; ++p)而不是for (; p!=nullptr; ++p) This correction is also documented in the errata for the book. 此修正也记录在本书的勘误表中。 Thanks! 谢谢!

Edit2: Sorry guys, this was apparently already asked on SO earlier here: Buggy code in "A Tour of C++" or non-compliant compiler? 编辑2:对不起,伙计们,这显然已经在早些时候问过: “C ++之旅”中的Buggy代码或不兼容的编译器?

No, a NULL pointer is not used to terminate strings. 不,NULL指针不用于终止字符串。 The NUL character is used. 使用NUL字符。 They are different things, but if you assign either of them to an integer in C, the result is always the same: zero (0). 它们是不同的东西,但是如果你将它们中的任何一个分配给C中的整数,结果总是相同的:零(0)。

A NUL character is represented in C as '\\0' and takes up one char of storage. NUL字符在C中表示为'\\0'并占用一个存储char A NULL pointer is represented in C as 0, and takes up the same storage as void * . NULL指针在C中表示为0,并占用与void *相同的存储void * For example, on a 64-bit machine, a void * is 8 bytes while '\\0' is one byte. 例如,在64位机器上, void *是8个字节,而'\\0'是一个字节。

Ie, nullptr is not the same thing as '\\0' . 即,nullptr与'\\0' And the character is the null character, called NUL, but it is not supposed to be called a NULL byte or a NULL character. 并且该字符是空字符,称为NUL,但它不应被称为NULL字节或NULL字符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM