Beginner question: How can I take the adress of a pointer and save it as an int? Example:
int *ptr = xyz;
int i = static_cast<int>(ptr);
So if ptr points to the memory adress 123, i should be 123. My compiler says it's an error: invalid static_cast from type 'int*' to type 'int'
.
Guess I am missing something but I don't know what.
You can use reinterpret_cast
. An int
is not guaranteed to be able to losslessly store a pointer though, so you should use the std::intptr_t
type instead.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With