i have following code
#include <iostream>
using namespace std;
int main(){
int i;
char *p="this is a string";
i=reinterpret_cast<int>(p);
cout<<i<<"\n":
return 0;
}
output is: 7648 please explain reinterpret_cast
reinterpret_cast<>
here will convert without checking the p
pointer to an int
but you can't make any assumption what this int
would represent. The only thing you can do, is convert this pointer back to what it was.
On a more practical note, it is likely that your compiler will put the address p
points to into the int
value, but int
size may not match a system pointer size, so you should probably use something like uintptr_t
for that instead of int
.
You can use something like Boost lexical_cast<>
which would try to convert a string-representation of an integer to the real integer value but I'm not sure that was your goal here.