2

I was posting an answer to a question - and commenting it occured to me, thanks to another poster by the name metal that

C++ compiler allowed this:

int *p = 0; but not this int *p = 1. Is 0 considered a special number?

Edit: @DavidHefferman said Is 0 special? In the context of a pointer, yes it is. - why?

Aniket Inge
  • 25,375
  • 5
  • 50
  • 78
  • 2
    Is 0 special? In the context of a pointer, yes it is. – David Heffernan Mar 21 '13 at 14:26
  • 0 is another representation of the null pointer constant.[See This](http://stackoverflow.com/questions/1296843/what-is-the-difference-between-null-0-and-0) – Suvarna Pattayil Mar 21 '13 at 14:28
  • It does allow `int* p = (int*)1;`. This will assign `1` to `int* p`. `0` doesn't need a type, while all other values do. That is what makes it special. – StarPilot Mar 21 '13 at 14:28
  • 1
    @StarPilot That's not the whole story. `int *p = 0;` is defined and useful and always works, whereas `int *p = (int *)1;` is at best implementation-defined (I'm not sure, it might also be UB), almost always useless, and almost always completely wrong. –  Mar 21 '13 at 14:37
  • Nulls, nils, null pointers, void, void*, etc, will all end up defined as `0`. `0` effectively has many types. – StarPilot Mar 21 '13 at 14:54
  • 2
    In the end, the only real answer is: because the standard says so. Historically, the inventors of C forgot to provide null pointers. And the first implementations allowed implicit conversions of ints to pointers, so people got into the habit of using `0` for a null pointer constant. C++ formalized this, but restricted it to integral constants evaluating to 0, and the standards committee of C took the C++ formalization (but added an additional legal possibility). – James Kanze Mar 21 '13 at 14:54
  • 2
    @StarPilot No. `0` has type `int`. Always. But C++, like C, has a lot of implicit conversions. This one is special, in that it only works for "integral expressions evaluating to 0". (Note that `NULL` can be defined as `(1 - 1)`, although I've never seen it. And for a long time, g++ defined it as `__builtin__nullptr`, or something like that---a compiler specific integral expression, which it could recognize, and generate a warning if it wasn't immediately converted to a pointer type.) – James Kanze Mar 21 '13 at 14:57

3 Answers3

6

Section 4.10 of the standard, Pointer conversions [conv.ptr] says:

A null pointer constant is an integral constant expression (5.19) 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 pointer to object or pointer to function type. Such a conversion is called a null pointer conversion. Two null pointer values of the same type shall compare equal. The conversion of a null pointer constant to a pointer to cv-qualified type is a single conversion, and not the sequence of a pointer conversion followed by a qualification conversion (4.4). A null pointer constant of integral type can be converted to a prvalue of type std::nullptr_t.

So, yes, 0 is a special value in the context of pointers.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • ^ Accepted answer in 6 minutes – Aniket Inge Mar 21 '13 at 14:33
  • 3
    Note, however, that *constant* expression is important -- for example, something like `int i=0; int *p=i;` will *not* work, because `i` is not a constant expression (even though it's pretty obvious from reading it that it'll always contain the value 0). – Jerry Coffin Mar 21 '13 at 14:40
  • @JerryCoffin Of course, you can force it with a `reinterpret_cast`. But even then, the results of the conversion may be different than those resulting from the conversion of a null pointer constant. – James Kanze Mar 21 '13 at 14:50
1

0 is NULL, while 1 is an invalid address.

TieDad
  • 9,143
  • 5
  • 32
  • 58
1

"A null-pointer constant is an integral constant expression that evaluates to zero (such as 0 or 0L)."

See http://www.cplusplus.com/reference/cstring/NULL/

tianz
  • 2,155
  • 2
  • 19
  • 28
  • Ie. the number `0` is a null-pointer constant and thus can be assigned to a pointer without an explicit type-cast. –  Mar 21 '13 at 14:31