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
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
reinterpret_cast<>here will convert without checking theppointer to anintbut you can’t make any assumption what thisintwould 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
ppoints to into theintvalue, butintsize may not match a system pointer size, so you should probably use something likeuintptr_tfor that instead ofint.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.