NULL和头文件的问题
三段测试代码如下:
//1
//没有 include 头文件 , 编译失败 -- NULL 未定义
using namespace std;
int main()
{
int a=NULL;
return 0;
}
//2
//引入 iostream , 编译成功
#include<iostream>
using namespace std;
int main()
{
int a=NULL;
return 0;
}
//3
//引入 vector , 编译成功
#include<vector>
using namespace std;
int main()
{
int a=NULL;
return 0;
}
提问:
1.上面编译成功的两段代码中所使用的 NULL 是在哪个头文件里定义的?
2.难道说 NULL 是在 istream 和 vector 的公共部分定义的 , 还是与编译器在处理 #include 时所执行的操作有关 ?
3.上面使用的 NULL 是 (void*)0 还是 (int)0 ,应该是后者吧?
盼望高人解答!
定义在stddef.h中,isstream与vector通过包含cstdef包含了stddef.h
定义如下
#undef NULL
#if defined(__cplusplus)
#define NULL 0
#else
#define NULL ((void *)0)
#endif
可以看出c++中 NULL为 (int)0 ,
C中NULL为 (void*)0