template<class IntType>
IntType atoi_unsafe(const char* source)
{
IntType result = IntType();
while (source)
{
auto t = *source;
result *= 10;
result += (*source - 48);
++source;
}
return result;
}
在 main()
中我有:
char* number = "14256";
atoi_unsafe<unsigned>(number);
但条件 while (source)
似乎无法识别 source
已遍历整个 C 字符串。它应该如何正确检查字符串的结尾?
最佳答案
while(source)
在指针环绕到 0 之前为真,但在现代系统中可能会在此之前崩溃。您需要取消引用指针以找到空字节,while(*source)
。
我讨厌发表简短的回答
关于c++ - 如何检查 char* p 是否到达 C 字符串的末尾?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6323993/