hello everyone!
what is the difference between NULL and 0 in C language?
NULL is a macro defined in <stdio.h>
It represents a null pointer constant, and may be defined either as 0 or as
((void *)0), which gives you a big hint that there's precious little
difference between them in semantic terms. The most important difference
is that 0 can definitely be used in contexts where an int is required,
whereas the possibility that NULL may be defined as ((void *)0) deters the
wary from using it in such contexts.
Thus, given this:
T *p = malloc(sizeof *p);
these are equivalent:
if(p != NULL)
and
if(p != 0)
They do the same thing with the same meaning.
But, given this:
int x;
these are not equivalent, or at least might not be:
x = 0;
and
x = NULL; /* if NULL is ((void *)0), which it can be,
this will cause a diagnostic message
*/
--
Richard Heathfield <http://www.cpax.org.uk >
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
On Wed, 29 Oct 2008 19:29:29 -0500, CBFalconer <cbfalconer@yah oo.com>
wrote in comp.lang.c:
iceman19860106 wrote:
what is the difference between NULL and 0 in C language?
>
NULL is a macro defining a pointer. The pointer points nowhere,
and thus cannot be dereferenced.
The statement above is sloppy to the point of being incorrect, Chuck.
NULL is a macro that represents a null pointer constant. It most
certainly NOT a pointer, it is a value that can be assigned to a
pointer object or compared against the value of a pointer object.
NULL in a source file is no more a pointer than a numeric literal '0'
is an int.
0 is a number. It represents an integer one less than 1. It is
usable in all integer expressions, except as a divisor.
Comment