Is NULL always equal to 0?
The answer depends on what you mean by “equal to.” If you mean “compares equal to,” such as
if ( /* ... */ )
{
p = NULL;
}
else
{
p = /* something else */;
}
/* ... */
if ( p == 0 )
then yes, NULL is always equal to 0. That’s the whole point of the definition of a null pointer.
If you mean “is stored the same way as an integer zero,” the answer is no, not necessarily. That’s the most
common way to store a null pointer. On some machines, a different representation is used.
The only way you’re likely to tell that a null pointer isn’t stored the same way as zero is by displaying a pointer
in a debugger, or printing it. (If you cast a null pointer to an integer type, that might also show a nonzero
value.)
Cross Reference:
VII.9: Is NULL always defined as 0?
VII.28: How do you print an address?
The answer depends on what you mean by “equal to.” If you mean “compares equal to,” such as
if ( /* ... */ )
{
p = NULL;
}
else
{
p = /* something else */;
}
/* ... */
if ( p == 0 )
then yes, NULL is always equal to 0. That’s the whole point of the definition of a null pointer.
If you mean “is stored the same way as an integer zero,” the answer is no, not necessarily. That’s the most
common way to store a null pointer. On some machines, a different representation is used.
The only way you’re likely to tell that a null pointer isn’t stored the same way as zero is by displaying a pointer
in a debugger, or printing it. (If you cast a null pointer to an integer type, that might also show a nonzero
value.)
Cross Reference:
VII.9: Is NULL always defined as 0?
VII.28: How do you print an address?
No comments:
Post a Comment