【发布时间】:2015-06-19 18:16:28
【问题描述】:
我认为指向NULL 的指针可以转换为0。于是我写了如下程序:
#include <iostream>
#include <regex>
#include <string>
#include <cstdlib>
bool is_strtoi(const std::string& str)
{
char *p;
std::strtol(str.c_str(), &p, 10);
return *p == '\0';
}
bool is_strtoi1(const std::string& str)
{
char *p;
std::strtol(str.c_str(), &p, 10);
return p; //Pointer to NULL should be converted to false;
}
int main ()
{
std::string test1("123asd2");
std::string test2("123123");
std::cout << is_strtoi1(test1) << std::endl;
std::cout << is_strtoi1(test2) << std::endl;
std::cout << "---" << std::endl;
std::cout << is_strtoi(test1) << std::endl; //prints 1
std::cout << is_strtoi(test2) << std::endl;
}
根据标准的 4.12 部分:
零值、空指针值或空成员指针值是 转为假
那为什么没有呢?
【问题讨论】:
-
我在你的代码中没有看到一个空指针。