了解用C NULL指针 [英] Understanding NULL pointer in C
问题描述
我曾经在一些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屋!