看板 C_and_CPP 關於我們 聯絡資訊
開發平台(Platform): (Ex: VC++, GCC, Linux, ...) C 問題(Question): char *aa; int *bb; short *cc; aa, bb, cc 的內容都是0,請寫出 *(aa+3), *(bb+6), *(cc+10) 所指到的位置 補充說明(Supplement): 只到的地方都NULL沒有宣告阿,是在問什麼? -- 網誌文章(慎入):http://ppt.cc/NYed 學習,就要思考它的價值:http://ppt.cc/NQrD 電競,是必要的嗎:http://ppt.cc/3n4g 鬥棋(小說):http://ppt.cc/WPqF 殘局PK戰:http://ppt.cc/FpGm -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 1.34.1.171 ※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1426214240.A.BD7.html
bben900911: 沒看錯的話 應該是問你三個char 六個int 十個short 03/13 10:45
bben900911: 多大吧..? 03/13 10:45
shadow0326: 想考你知不知道指標上的加號跟指標類型有關 03/13 12:04
shadow0326: 不過他的問法滿奇怪的 03/13 12:05
james732: http://ideone.com/lRxPEI 大概就是這個意思吧? 03/13 12:51
EdisonX: aa=3 bb=24 cc=20,猜要的是這個 03/13 17:46
OPIV: *(aa+3)什麼的的型態又不是指標 怎麼會問指到哪裡= = 03/13 19:29
littleshan: 這些全是undefined behavior 03/14 00:06
Killercat: 我猜他想問的是&(*(aa+3))吧? 03/14 01:17
Killercat: 只是的確都是undefined behavior... XD 根本不知道有沒 03/14 01:17
Killercat: 有吃到不該吃的記憶體 03/14 01:17
Killercat: * = 提取,這動作會要命的 03/14 01:18
suhorng: 但是就算不提取, 只要原本不是指到正確的真實存在的東西, 03/14 01:23
suhorng: 而且索引在範圍內, 就未定義吧 03/14 01:24
Killercat: 指過去不會有事 提取才會有事 03/14 02:29
Killercat: 基本上你喜歡高興愛int *i = 1234567也是可以的... 03/14 02:29
Killercat: 當然int是不能賦值給pointer啦 不過 int *i = NULL; 03/14 02:32
Killercat: i += 10; 基本上都不會出事,只有在提取的時候會炸 03/14 02:32
Killercat: 比方說cout << *i; 就死了 03/14 02:33
Killercat: 「亂指沒關係,但是不能提取一個不該提取的位置」 03/14 02:33
suhorng: 不是 undefined behavior 嗎? 03/14 02:34
Killercat: 但是你單純cout << i;你會發現它指標正確的指到0x28 03/14 02:34
Killercat: undefined behavior指的是「提取不該提取的指標」 03/14 02:35
suhorng: n4296 5.7.4 這邊定義運算時只有針對陣列才有定義的樣子? 03/14 02:36
suhorng: 而且運算只能在 array bounds 中, 或剛好超過一格 03/14 02:36
Killercat: maa, 你的意思是說*(aa+3)如果移動3*sizeof(aa)要是 03/14 02:38
Killercat: out of bound的話就會變成undefined嗎? 03/14 02:38
Killercat: 我的認知是aa+3仍然有效 但是*(aa+3)的* (提取)是UB 03/14 02:39
Killercat: 但是他說的是「指到的位置」(有點語焉不詳的感覺) 03/14 02:40
Feis: 照標準應該是 aa 跟 aa+1 合法, aa+3 是 UB 03/14 13:02
Feis: 還是我誤會了些甚麼. aa 到底是不是指標 xD 03/14 13:04
Feis: 我看到 3*sizeof(aa) 有點疑惑 03/14 13:04
Feis: 阿. 我的前提是 aa 真的要指到東西啦.. Orz 03/14 13:06
Feis: 如果 aa 有指到東西 aa+1 就合法, 但是 aa+3 不行 03/14 13:06
Feis: 但是 *(aa+1) 也是 UB 03/14 13:10
Feis: 如果 aa 是亂指的話, aa+1, aa+3 都不行. 加 * 就都不行 03/14 13:11
suhorng: 剛好超過一格好像還可以XD 假設 aa 指到長度為 3 03/14 13:12
suhorng: 那 aa, aa+1, aa+2, aa+3 (剛好超過一格)好像都可 03/14 13:12
Feis: 原則上就是要指到同一個陣列內或陣列後一格 03/14 13:14
Feis: 如果指向的不是陣列的話, 會當作只有一個元素的陣列 03/14 13:15
Feis: suhorng: 我上面其實是想解釋如果指向的不在陣列內 03/14 13:22
Feis: 如果 i 是 null pointer, 那 i + 10 是未定義 03/14 13:23
suhorng: i see 03/14 13:24
Feis: 不過這個問題不已經是 FAQ 了嗎. Q_Q 03/14 13:31