Reply To: NULL Pointer and NUL in C programming.

Home Forums C-programming.. NULL Pointer and NUL in C programming. Reply To: NULL Pointer and NUL in C programming.

#3410
Humble
Keymaster

#include
int main()
{
int *p = NULL;

if (p == NULL)
{
printf (“p is NULL\n”);
}
else
{
printf (“p is not NULL\n”);

}
}

Above produce below result :

[hchiramm@humbles-lap ]$ ./a.out
p is NULL

But, if we have below code :

#include
int main()
{
int *p = NULL;
if (p)
{
printf (“p is NULL\n”);
}
else
{
printf (“p is not NULL\n”);

}
}

[hchiramm@humbles-lap ]$ ./a.out
p is not NULL

Because NULL is represented as binary zero in a system, that said, binary zero is treated as “false” in C, so else clause is getting executed..

In standard libike this:

#define NULL ((void*)0)

Dereferencing a NULL pointer will result in ‘segmentation fault” .