了解用C NULL指针 [英] Understanding NULL pointer in C

查看:90
本文介绍了了解用C NULL指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我曾经在一些code NULL 指针被定义发现如下 -

 的#define NULL((的char *)0)

我发现这些code编译罚款。但我不明白这是如何工作。任何人都可以请解释如何0铸造到字符指针?

和它是有效的使用它作为一个文件指针制造空 -

  FILE * FP = NULL;


解决方案

C库宏NULL是一个空指针constant.It的值可以定义为((无效*)0)0或0L 取决于编译器供应商。
根据编译器,NULL的声明可以是

 的#define NULL((的char *)0)

 的#define NULL 0L

 的#define NULL 0


  

和它是有效的使用它作为一个文件指针制造空 - >
  是的。


I have found in some code NULL pointer is defined as follows -

#define NULL ((char *)0)

I found these code compiles fine. But I did not understand how this works. Can anyone please explain how 0 is casted to a char pointer?

And is it valid to use it as a FILE pointer making null -

FILE *fp = NULL;

解决方案

The C library Macro NULL is the value of a null pointer constant.It may be defined as ((void*)0), 0 or 0L depending on the compiler vendor. Depending on compiler,declaration of NULL can be

#define NULL ((char *)0)

or

#define NULL 0L

or

#define NULL 0

And is it valid to use it as a FILE pointer making null--> Yes.

这篇关于了解用C NULL指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆