Reputation: 4861
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. :)
Upvotes: 0
Views: 209
Reputation: 474
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
Upvotes: 2
Reputation: 350
In C, two null pointers of any type are guaranteed to compare equal. The macro NULL
is defined as an implementation-defined null pointer constant, which in C99
can be portably expressed as the integer value 0 converted implicitly or explicitly to the type void *
.
Upvotes: 7