Understanding NULL pointer in C

2019-06-23 23:44发布

问题:

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;

回答1:

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 pointers null