How do I cast a pointer to an int
You can do this:
int a_variable = 0;
int* ptr = &a_variable;
size_t ptrValue = reinterpret_cast<size_t>(ptr);
int
may not be large enough to store a pointer.
You should be using intptr_t
. This is an integer type that is explicitly large enough to hold any pointer.
intptr_t thatvalue = 1;
// stuff
thatvalue = reinterpret_cast<intptr_t>(ip);
// Convert it as a bit pattern.
// It is valid and converting it back to a pointer is also OK
// But if you modify it all bets are off (you need to be very careful).