""空字符串,会创建一个对象,分配内存空间

NULL,不会创建对象,不会分配内存空间,意思是不存在的


#include<iostream>usingnamespacestd;intmain(){std::stringstrTemp;if(strTemp.empty()){cout<<"isempty"<<endl;}else{cout<<"isnotempty"<<endl;}strTemp="";if(strTemp.empty()){cout<<"isempty"<<endl;}else{cout<<"isnotempty"<<endl;}if(strTemp==""){cout<<"isempty"<<endl;}else{cout<<"isnotempty"<<endl;}/*if(strTemp==NULL){cout<<"isempty"<<endl;}else{cout<<"isnotempty"<<endl;}*/return0;}

在以下代码最后,用NULL与字符类型进行比较,会直接导致编译错误!

test.cpp:37:17: error: no match for ‘operator==’ in ‘strTemp == 0’

从编译的错误提示看:NULL为数值0


#include<iostream>usingnamespacestd;intmain(){if(NULL==0){cout<<"NULLisnumber0"<<endl;}else{cout<<"NULLisnotnumber0"<<endl;}intnNum;cout<<"nNUm="<<nNum<<endl;nNum=NULL;cout<<"nNUm="<<nNum<<endl;cout<<static_cast<void*>(NULL)<<endl;return0;}

从这段代码,进一步验证了,NULL对就的值为数值0,地址为空

注意:将NULL赋值为int变量时会报警告

NULL.cpp:18:9: warning: converting to non-pointer type ‘int’ from NULL [-Wconversion-null]