函数TrimRight占用一行并删除末尾的所有空格。
void TrimRight(char *s) // input "somestring " (3 spaces at the end)
{
// Here s == "somestring \0" and strlen(s) == 14
int last = strlen(s) - 2;
std::cout << "Last1: " << s[last] << std::endl; // Last == ' '
while (s[last] == ' ')
{
--last;
}
std::cout << "Last2: " << s[last] << std::endl; // Last == 'g'
s[last + 1] = '\0';
// Here s == "somestring" and strlen(s) == 10
}
问题是为什么在TrimRight之后s!=“somestring / 0”? 我正在使用MVS 2017.谢谢。