[問題] 請問一題課本code範例

看板C_and_CPP (C/C++)作者時間13年前 (2012/08/03 15:11), 編輯推噓5(5017)
留言22則, 7人參與, 最新討論串1/1
開發平台(Platform): (Ex: VC++, GCC, Linux, ...) Ubuntu 10.04 額外使用到的函數庫(Library Used): (Ex: OpenGL, ...) 問題(Question): 我想應該是型態 int 與 int * 型態不同, 也可能是編譯器版本與課本不同 請問版上大大該如何改code 或是 調整其她部份 謝謝 <(_ _)>~~ 餵入的資料(Input): gcc pnt_add.c 預期的正確結果(Expected Output): pointers + 0: 56014 56026 pointers + 1: 56016 56030 pointers + 2: 56018 56034 pointers + 3: 56020 56038 錯誤結果(Wrong Output): pnt_add.c: In function ‘main’: pnt_add.c:12: warning: format ‘%10u’ expects type ‘int’, but argument 3 has type ‘int *’ pnt_add.c:12: warning: format ‘%10u’ expects type ‘int’, but argument 4 has type ‘float *’ 程式碼(Code):(請善用置底文網頁, 記得排版) // pnt_add.c -- pointer addition #include<stdio.h> #define SIZE 4 int main(void) { int dates[SIZE], *pti , index; float bills[SIZE], *ptf; pti = dates; ptf = bills; for ( index = 0; index < SIZE; index ++ ) printf( "pointers + %d: %10u %10u\n", index, pti + index, ptf + index ); return 0; } 補充說明(Supplement): -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 1.174.118.46

08/03 15:15, , 1F
加括號? 可是dates跟bills什麼都沒有要輸出嗎?
08/03 15:15, 1F

08/03 15:16, , 2F
應該說dates & bills裡面的東西沒有意義啦
08/03 15:16, 2F

08/03 15:16, , 3F
*(pti + index) ?
08/03 15:16, 3F

08/03 16:07, , 4F
你那什麼時候的課本 他有沒有針對哪一種compiler
08/03 16:07, 4F

08/03 16:08, , 5F
他明明就是要看位址而已 一樓不要亂抓藥
08/03 16:08, 5F

08/03 16:47, , 6F
你的compiler不是只有吐warning給你嗎?
08/03 16:47, 6F

08/03 18:28, , 7F
抱歉QAQ
08/03 18:28, 7F

08/03 19:25, , 8F
要印位址的話,%10u 改成 0x%p 或 0x%08X
08/03 19:25, 8F

08/03 20:16, , 9F
課本是 The Waite Group's New C Primer Plus
08/03 20:16, 9F

08/03 20:21, , 10F
編譯器為 Microsoft C 5.1 版本
08/03 20:21, 10F

08/03 20:43, , 11F
這是..解嚴前的編譯器?
08/03 20:43, 11F

08/03 21:41, , 12F
幹這不是我小時候的compiler 小蔣死掉那一年發行的 >(=)
08/03 21:41, 12F

08/03 21:42, , 13F
拜託你別用這古董了吧 C Primer Plus有新版 一般也建議
08/03 21:42, 13F

08/03 21:42, , 14F
Primer Plus要看過C Primer再看比較適當
08/03 21:42, 14F

08/03 21:49, , 15F
沒想到我看的書那麼有年紀 Orz
08/03 21:49, 15F

08/03 21:55, , 16F
找一下版權頁,看他是第幾edition哪一年出的
08/03 21:55, 16F

08/03 21:56, , 17F
上面sax兄給你指點了,照著修改可印出指標,但是16進位
08/03 21:56, 17F

08/03 21:57, , 18F
compiler也未必每次給一樣位址,只要觀察間隔為4就行了
08/03 21:57, 18F

08/03 23:18, , 19F
看! 解嚴前我還沒出生耶= =
08/03 23:18, 19F

08/04 00:11, , 20F
解嚴了啦,小蔣死掉前一年七月解嚴。
08/04 00:11, 20F

08/04 00:17, , 21F
這code根本不用改就能編過啊...只是會吐warning而已....
08/04 00:17, 21F

08/05 13:50, , 22F
感謝樓上各位大大!!!!
08/05 13:50, 22F
文章代碼(AID): #1G6tc4q0 (C_and_CPP)
文章代碼(AID): #1G6tc4q0 (C_and_CPP)