Is it possible to have a pointer literal?

前端 未结 3 1127
夕颜
夕颜 2020-12-10 14:37

In C one can have string literals in the form of

char *string = \"string here\";

integer literals:

uint8_t num = 5;
         


        
相关标签:
3条回答
  • 2020-12-10 15:01

    In C++ could you do something like the following?

    uint32_t * source = reinterpret_cast<uint32_t*>(0x08000000);
    
    0 讨论(0)
  • 2020-12-10 15:17

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

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

    and:

    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 3.7.4.3, the result of such a conversion will not be a safely-derived pointer value. —end note ]

    You need to see section 3.7.4.3 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 ]

    0 讨论(0)
  • 2020-12-10 15:22

    No, it's not. That is because literals are valid values, and the only valid pointers are addresses of objects, i.e. the result of address-of operations or of pointer arithmetic on valid pointers.

    You could argue that the nullptr keyword furnishes a kind of "pointer literal"; the C++ standard calls it that. It is however the only pointer literal, and ironically it is not of pointer type.

    0 讨论(0)
提交回复
热议问题