c++转义字符在osx和windows中的差异

C++ escape character difference in osx and windows?

本文关键字:windows 转义字符 osx c++      更新时间:2023-10-16

我在xcode中使用c++,我遇到了一个问题

char array[] = "Apple"; 

从技术上讲,会自动添加空转义''。我只是写了一个基本的函数来添加c字符串中的字母数量,但我进入了一个永远的循环,因为它找不到''。为什么呢?我用的是g++。

int CharLength(char* word)
{
    char* temp = word;
    int count = 0 ;
    while ( temp != '' )
    {
        temp++;
        count++;
    }
    return count;
}

您忘记取消引用了

while ( *temp != '' )

" temp "不会为0,直到您查看了所有内存并进行了封装!另一方面,当你到达字符串的末尾时,*temp将为零。循环条件应为

while ( *temp != '' )

头中使用strlen