NULL
itself is not a pointer, it is a macro that can be used to initialize a pointer to the null pointer value of its type. When compared to a pointer, it compares equal if the pointer is a null pointer and unequal if the pointer is a valid pointer to an object of its type.
There is no semantic difference between char *p = 0;
and char *p = NULL;
but the latter is more explicit and using NULL
instead of 0
is more informative in circumstances where the other operand is not obviously a pointer or if comparing to an integer looks like a type mismatch:
FILE *fp = fopen("myfile", "r");
if (fp == NULL) {
/* report the error */
}
Similarly, there is no semantical difference in C between '\0'
and 0
, they both are int
constants. The first is the null byte, the second the null value. Using 0
, '\0'
and NULL
wisely may seem futile but makes code more readable by other programmers and oneself too.
The confusion may come from misspelling or mishearing the null pointer as the NULL
pointer. The C Standard was carefully proof read to only use null pointer and refer to NULL
only as the macro NULL
.
Note however that one the accepted definitions of NULL
, #define NULL ((void*)0)
makes NULL
a null pointer to void
.
(void *)
in POSIX. – Alveolate