我在"A Tour of C++"中看到了以下函数,第12页:
int count_x(char const* p, char x)
{
int count = 0;
while (p)
{
if (*p == x) ++count;
++p;
}
return count;
}
Run Code Online (Sandbox Code Playgroud)
这条线while (p)听起来不对我.我以为它应该是while (*p).但是,不想过于放肆,我用下面的代码测试了这个函数.
int main(int argc, char** argv)
{
char* p = argv[1];
char x = argv[2][0];
int count = count_x(p, x);
std::cout
<< "Number of occurences of "
<< x << " in " << p << ": " << count << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
当我运行该程序时,它退出了 …