C++ assigning integer (containing address) to pointer


I am new to C++, I have a function which returns address of a memory location and I want to assign that address to a pointer, just like:

unsigned int address = 0xdeadbeef;
unsigned int* memory_ptr = (unsigned int*) address;

But above code is through warning:

cast to pointer from integer of difference size [-Wint-to-pointer-cast] 

Is there anyway to do this task cleanly in C++ ?

asked on Stack Overflow Jan 3, 2019 by Ameer Hamza

1 Answer


Instead of unsigned int you can use uintptr_t.

Integer type capable of holding a value converted from a void pointer and then be converted back to that type with a value that compares equal to the original pointer.

answered on Stack Overflow Jan 3, 2019 by Yola

User contributions licensed under CC BY-SA 3.0