Like consider I declare two pointers NULL. Then if I compare them, the result is true. But if we consider NULL to be "nothing" then how can we say that two "nothings" are equal? Is that for some specific reason? Any help appreciated. :)
2 Answers
When you point pointer to NULL then it means that it is pointing to the memory address 0x0.
Consider following example,
#include <stdio.h>
int main()
{
char *p = NULL;
..
..
Here, *p is pointing to NULL. If we check in gdb then we will get following,
(gdb) p p
$1 = 0x0
0x0 is the address of NULL value i.e. 0. You cannot access 0x0 memory address.
(gdb) p *p
Cannot access memory at address 0x0
If you try to access memory address 0x0 then program will be crashed by saying segmentation fault
Now if you compare two pointers which are pointing to memory address 0x0 i.e. NULL then it will be same only as the NULL value is 0 by default
5. If I compare them, they're equal. The same idea applies to pointers.