我試圖將char *再次加倍並轉換回char *。如果您創建的應用程序是32位,但不適用於64位應用程序,則以下代碼正常工作。當您嘗試從int轉換回char *時,會發生此問題。例如,如果hello = 0x000000013fcf7888,則轉換爲0x000000003fcf7888,只有最後的32位是正確的。char *再次加倍並返回char *(64位應用程序)
#include <iostream>
#include <stdlib.h>
#include <tchar.h>
using namespace std;
int _tmain(int argc, _TCHAR* argv[]){
char* hello = "hello";
unsigned int hello_to_int = (unsigned int)hello;
double hello_to_double = (double)hello_to_int;
cout<<hello<<endl;
cout<<hello_to_int<<"\n"<<hello_to_double<<endl;
unsigned int converted_int = (unsigned int)hello_to_double;
char* converted = reinterpret_cast<char*>(converted_int);
cout<<converted_int<<"\n"<<converted<<endl;
getchar();
return 0;
}
這些轉換是無用的和危險的(將*指針*轉換爲浮點數的想法讓我感到畏縮)。你應該避免它們。 –
@ user965772你爲什麼這樣做? – Praetorian
鑄造是非常危險的,當你理解它時... – hplbsh