作者s9642108 (sss)
看板C_and_CPP
標題[問題] 有關 short 轉double 問題
時間Tue Jul 31 02:55:17 2018
開發平台(Platform): (Ex: Win10, Linux, ...)
QT 5.10
編譯器(Ex: GCC, clang, VC++...)+目標環境(跟開發平台不同的話需列出)
qt creator4.5.1
額外使用到的函數庫(Library Used): (Ex: OpenGL, ...)
問題(Question):
short tempData[10];
*(int *)tempData = 1;
*(double *)&tempData[1]=100;
qDebug() << " " << tempData[0];
qDebug() << " " << tempData[1];
請問為什麼 轉int結果是對的
但是double 卻是0呢
c++新手 求各位幫幫我 感謝
其中qDebug 相當於 cout
預期的正確結果(Expected Output):
1
100
錯誤結果(Wrong Output):
1
0
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 218.35.154.241
※ 文章網址: https://webptt.com/m.aspx?n=bbs/C_and_CPP/M.1532976920.A.C56.html
1F:噓 Hazukashiine: 你 warning 有看了沒? 07/31 04:14
2F:→ Hazukashiine: short* 硬轉 double* 我不信編譯器沒報警告 07/31 04:15
3F:→ Hazukashiine: 轉 int* 會對是 data model 設計的寬度一樣而已 07/31 04:17
4F:推 wei115: ieee754 07/31 04:41
5F:→ MOONRAKER: 真有創意的轉法。 07/31 15:34
6F:→ adrianshum: 轉int會成功只是因為你在little endian 的平台吧。 bi 08/01 20:47
7F:→ adrianshum: g endian 會出現0 (不 core dump 的話) 08/01 20:47