, C, if , zero non-zero. NULL 0, if! , NULL
Proof
#include <stdio.h>
int main()
{
int *p = NULL;
int a = 1;
printf("%p %d\n", p, p);
p = &a;
printf("%p %d\n", p, p);
return 0;
}
, :
$> gcc null.c
$> ./a.out
(nil) 0
0x7fff0556ff3c 89587516
, printf, % d, p zero. , if, , NULL -NULL.
In addition, I would like to add that use is made from standard best practices. This is the preferred method of checking if the pointer is NULL compared to potentially dangerous and error prone:
if (p == NULL) {
}
which may be mistakenly entered (dangerous) in:
if (p = NULL) {
}
source
share