Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is it okay to compare a pointer with '\0'? (but not 'A')

Tags:

c++

I found a bug in my code where I compared the pointer with '\0'.

Wondering why the compiler didn't warn me about this bug I tried the following.

#include <cassert>

struct Foo
{
    char bar[5];
};

int main()
{
    Foo f;
    Foo* p = &f;
    p->bar[0] = '\0';
    assert(p->bar == '\0');    // #1. I forgot [] Now, comparing pointer with NULL and fails.
    assert(p->bar == 'A');     // #2. error: ISO C++ forbids comparison between pointer and integer
    assert(p->bar[0] == '\0'); // #3. What I intended, PASSES
    return 0;
}

What is special about '\0' which makes #1 legal and #2 illegal?

Please add a reference or quotation to your answer.

like image 511
Eddy Pronk Avatar asked Dec 22 '11 07:12

Eddy Pronk


People also ask

Why is a pointer with a value of 0 Not a valid address?

Because you can only compare pointers against each other and against this special-value-0, it insulates the programmer from knowing anything about the memory representation of the system.

Is it possible to compare two pointers Why?

In Go language, you are allowed to compare two pointers with each other. Two pointers values are only equal when they point to the same value in the memory or if they are nil. You can perform a comparison on pointers with the help of == and !=

Is null and '\ 0 the same?

'\0' is defined to be a null character. It is a character with all bits set to zero. This has nothing to do with pointers. '\0' is (like all character literals) an integer constant with the value zero.

Can you compare pointers with ==?

The == operator on pointers will compare their numeric address and hence determine if they point to the same object.


2 Answers

What makes it legal and well defined is the fact that '\0' is a null pointer constant so it can be converted to any pointer type to make a null pointer value.

ISO/IEC 14882:2011 4.10 [conv.ptr] / 1:

A null pointer constant is an integral constant expression prvalue of integer type that evaluates to zero or a prvalue of type std::nullptr_t. A null pointer constant can be converted to a pointer type; the result is the null pointer value of that type and is distinguishable from every other value of object pointer or function pointer type. Such a conversion is called a null pointer conversion.

'\0' meets the requirements of "integral constant expression prvalue of integer type that evaluates to zero" because char is an integer type and \0 has the value zero.

Other integers can only be explicitly converted to a pointer type via a reinterpret_cast and the result is only meaningful if the integer was the result of converting a valid pointer to an integer type of sufficient size.

like image 112
CB Bailey Avatar answered Sep 20 '22 19:09

CB Bailey


'\0' is simply a different way of writing 0. I would guess that this is legal comparing pointers to 0 makes sense, no matter how you wrote the 0, while there is almost never any valid meaning to comparing a pointer to any other non-pointer type.

like image 30
Nathan Fellman Avatar answered Sep 20 '22 19:09

Nathan Fellman