The second parameter to memset
is of type int
. NULL
is the null pointer constant in C. According to the standard:
An integer constant expression with the value 0, or such an expression cast to type void *
, is called a null pointer constant.
So, NULL
may be defined as (void *)0
or equivalent. In such a case, your memset
call is wrong because you are converting a pointer to an integer.
The other way is okay: if you use 0
where a null pointer is expected, the compiler will do the conversion for you. So, given a type T
, the following sets both p
and q
to the null pointer:
T *p = NULL;
T *q = 0;
Further, given a pointer p
, the following are equivalent:
if (p) ...
if (p != NULL) ...
if (p != 0) ...
This is because 0
is special: in pointer contexts, it is automatically converted to the null pointer constant. A pointer on the other hand, is never converted to an integer implicitly. Any integer other than 0
is also never converted to a pointer implicitly.
Finally, in the code below, even though i
has the value 0
, it is not a null pointer constant in the assignment to p
:
int i = 0;
int *p = i;
The above will trigger a warning when compiled in conformant mode.
(The above is a bit of a digression, but I hope it is not completely off-topic.)