【发布时间】:2020-06-07 20:05:08
【问题描述】:
在尝试使用空指针时,我尝试编写代码来检查指针是否为空:
#include <iostream>
using namespace std ;
int main(){
int *p = 0;
if(!p == true){
cout<<"nullptr\n";
}
else{
cout<< "no\n";
}
return 0;
}
它工作得很好,没有任何错误,但是当我尝试这个时(根据我的说法基本上是一样的,但显然不是):
#include <iostream>
using namespace std ;
int main(){
int *p = 0;
if(p == false){
cout<<"nullptr\n";
}
else{
cout<< "no\n";
}
return 0;
}
我收到一个错误:“错误:ISO C++ 禁止在指针和指针之间进行比较 整数 [-fpermissive]"
谁能告诉我这两个代码有何不同以及为什么我在第二个代码中出现错误?
【问题讨论】:
-
!p是一个布尔值。p是int*。