NULL
and \0
are not the same thing, though they both evaluate to zero. NULL
is a pointer zero, i.e. it's what we use for a null pointer. \0
is the character with ASCII number zero, otherwise known as NUL
(one 'L'), i.e. is a char
with value 0.
There is an important difference between a char *
being NULL
(i.e. there not being a string in memory at all), or the string being empty (i.e. only containing one char
which is \0
otherwise known as NUL
).
To test for the first:
if (!string)
or if you like to be more verbose:
if (string == NULL)
To test for the second:
if (!string[0])
or if you like to be more verbose:
if (string[0] == 0)
Obviously if you need to test for both, test for the first then the second, as the second would dereference a null pointer if string
was NULL
.