三段测试代码如下:

//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

相关文章:

  • 2022-02-17
  • 2022-12-23
  • 2022-12-23
  • 2021-12-06
  • 2021-10-16
  • 2021-06-14
  • 2021-12-03
  • 2021-06-01
猜你喜欢
  • 2022-12-23
  • 2021-09-03
  • 2021-11-29
  • 2022-12-23
  • 2021-12-12
  • 2021-11-02
  • 2021-11-20
相关资源
相似解决方案