About Initializing Pointers in C++

Why assigning to a int pointer to 0 is fine

Because 0, besides being an integer literal, is also a null pointer constant. And a null pointer constant converts to any pointer type.

but 123 is not ok

Because 123, or any other integer literal besides those whose value is 0, is not a null pointer constant. And integer expressions (except for null pointer constants) don't implicitly convert to pointer types.

Why does 123 result in a to "*int" cast, while 0 does not?

Neither "results in a cast". Cast is an explicit conversion, and in these examples all conversions are implicit. Implicit conversion from integer to pointer is ill-formed, which is why you get the error. The null pointer conversion is implicit and well-formed, which is why you don't get an error.

Can someone help me explain the exact rule

Here is the exact rule (quote from the latest C++ standard draft):

[conv.ptr]

A null pointer constant is an integer literal ([lex.icon]) with value 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 ([basic.compound]) and is distinguishable from every other value of object pointer or function pointer type. Such a conversion is called a null pointer conversion. ...

P.S. The keyword nullptr is a prvalue of type std​::​nullptr_­t. It should always be favoured over using literal 0 except in the case you intend to support pre-C++11 compilers.


C++ has a special case for the literal integer 0, which is implicitly convertible to a null pointer.

Tags:

C++

Pointers