[問題] char and int 的關係轉換程式

看板C_and_CPP作者時間14年前 (2010/12/21 07:42), 編輯推噓0(003)
留言3則, 3人參與, 最新討論串1/1
開發平台(Platform): (Ex: VC++, Gcc, Linux, ...) dev-c++ or visual c++ 額外使用到的庫(Library Used) (Ex: OpenGL, ...) NO 問題(Question): 兩個程式的理論結果應該要一樣,但是實際結果卻不一樣。 餵入的資料(Input): NO 預期的正確結果(Expected Output): 100 100 (我只有標後面*i2 *c2兩個重點) 錯誤結果(Wrong Output): 亂數 100 程式碼(Code): (請善用置底文標色功能) #include <stdio.h> int main() { int i1[4] = { 100, 102, 104, 106}; char *c1; c1 = (char *)i1; printf("%x %x %d %d ", i1, c1, *i1, *c1); char c2[4] = { 100, 102, 104, 106}; int *i2 = 0; i2 = (int *)c2; printf("%x %x %d %d ", i2, c2, *i2, *c2); return 0; } 補充說明(Supplement): 我用了兩個編譯器,雖然兩台編譯器的位址不同, 但是那個有問題的*i2的值會相同。 請高手幫忙解 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 61.230.52.158

12/21 16:30, , 1F
已解決,多謝大家
12/21 16:30, 1F

12/21 20:30, , 2F
已解決是怎樣解決要說啊....
12/21 20:30, 2F

12/21 21:36, , 3F
應是類似加上 *i2 &= 0xff; 處理. extension 問題
12/21 21:36, 3F
文章代碼(AID): #1D45fnBb (C_and_CPP)