Is it possible to have a pointer literal?

In both C and C++ the only pointer literal or constant is zero. We can go to the draft C99 standard section Pointers:

An integer constant expression with the value 0, or such an expression
cast to type void *, is called a null pointer constant.55)


An integer may be converted to any pointer type. Except as previously
specified, the result is implementation-defined, might not be
correctly aligned, might not point to an entity of the referenced
type, and might be a trap representation.56)

the correct way to deal with non-zero integer constant is to use a cast.

The equivalent section from the draft C++ standard would probably be section 5.2.10 Reinterpret cast which says:

A value of integral type or enumeration type can be explicitly
converted to a pointer. A pointer converted to an integer of
sufficient size (if any such exists on the implementation) and back to
the same pointer type will have its original value; mappings between
pointers and integers are otherwise implementation-defined. [ Note:
Except as described in, the result of such a conversion will
not be a safely-derived pointer value. —end note ]

You need to see section for all the details.

For the pointer literal reference you need section 2.14.7 Pointer literals which says:

The pointer literal is the keyword nullptr. It is a prvalue of type
std::nullptr_t. [ Note: std::nullptr_t is a distinct type that is
neither a pointer type nor a pointer to member type; rather, a prvalue
of this type is a null pointer constant and can be converted to a null
pointer value or null member pointer value. See 4.10 and 4.11. —end
note ]

Leave a Comment