Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to have a pointer literal?

In C one can have string literals in the form of

char *string = "string here";

integer literals:

uint8_t num = 5;

long literals:

long long bigNum = 90322L; 

floating point literals:

float decimal = 6.3f;

Is the a way to have a pointer literal? That is a literal address to a memory space. I am doing some work on an embedded project and need to hard code a value for a DMA access. I am doing something similar to the following:

uint32_t *source = 0x08000000;

While this compiles and works correctly I get the following compiler error (I'm using a variant of GCC):

cc0144: {D} warning: a value of type "int" cannot be used to initialize an entity of type "uint32_t *"
cc0152: {D} warning: conversion of nonzero integer to pointer

Is there a correct way to do this or do I just need to accept this as a fact of C? I know I can do:

uint32_t *source = (uint32_t *)0x08000000;

But that just seems very unnecessary. What is the industry way of doing this? I am also wondering if this feature exists in C++.

like image 983
secretformula Avatar asked Jun 13 '14 19:06

secretformula


People also ask

Can a pointer point to a literal?

In the C++ view of the world, a literal does not occupy any memory. A literal just exists. This view makes that there is no address for a pointer to refer to when it would point to a literal and for that reason, pointers to literals are forbidden.

Can integer literals be assigned to pointers?

You can assign 0 into a pointer: ptr = 0; The null pointer is the only integer literal that may be assigned to a pointer.

Can a pointer refer to any type?

Pointers are typed to indicate the kind of data stored at the addresses they hold. The general-purpose Pointer type can represent a pointer to any data, while more specialized pointer types reference only specific types of data. The PByte type is used for any byte data that is not character data.

Can a pointer point to multiple things?

Yes. A pointer p to type T can point to a single T , or to an array of T . In the latter case you can index into the array using pointer arithmetics, such as p[n] .


1 Answers

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 ]

like image 120
Shafik Yaghmour Avatar answered Sep 25 '22 17:09

Shafik Yaghmour