How to initialize a pointer to a specific memory address in C++

This will work:

void *address=(void *) 0xdead; // But as mentioned, it's non-standard

address=(void *) 0xdeadbeef; // Some other address

In C++, always prefer reinterpret_cast over a C-cast. It's so butt ugly that someone will immediately spot the danger.

Example:

int* ptr = reinterpret_cast<int*>(0x12345678);

That thing hurts my eyes, and I like it.


In C++, I prefer to declare the pointers as constant pointers in a header file:

volatile uint8_t * const UART_STATUS_REGISTER = (uint8_t *) 0xFFFF4000;

In the C language, this is usually implemented using a macro:

#define UART_STATUS_REGISTER ((volatile uint8_t * const) 0xFFFF4000)

In the rest of the source code, the memory address is referenced via the symbolic name.


There is NO standard and portable way to do so. Non-portable ways may include reinterpret_cast(someIntRepresentingTheAddress).