看板 C_and_CPP 關於我們 聯絡資訊
各位朋友們好,最近我在研究雙重指標的用法,有個問題一直想不出來為什麼 Code: int main() { char *name = "Rick"; char **pp = &name; printf("string Rick is stored in %p\n", name); printf("name's address is in %p\n", &name); printf(" **pp's address is %p\n", &pp); printf(" *pp with %/p = %p\n", *pp);  printf(" *pp with %/s = %s\n", *pp);  //這行反倒可以解出內容 ’Rick’ printf(" **pp's dereference = %s\n", **pp);  // 我想解出內容,但出現警告與Segmentaion fault Result: string Rick is stored in 0x8048530 name's address is in 0xbf9e9c9c **pp's address is 0xbf9e9c98 *pp with %/p = 0x8048530 *pp with %/s = Rick Segmentation fault 為什麼Rick是使用 *pp解出來而不是**pp呢? 為什麼 *pp可以同時解出字串存放的位置跟字串內容呢?  謝謝 -- Sent from my Windows -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 36.228.2.202 ※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1491838496.A.4FE.html
LPH66: 嗯...如果你只是想要理解雙重指標的話, 用個整數變數吧 04/10 23:57
LPH66: 在這裡摻進字串變數跟字元陣列的關係會搞得更亂 04/10 23:57
LPH66: 簡言之, C 字串在很多地方跟字元陣列很像 04/10 23:58
kokal: %s要接pointer, dereference兩次就爆掉啦 04/11 00:04
c910335: 把 %s 換成 %c 跑跑看你應該就能理解了 04/11 05:30
outofyou: 其實測試Result足夠給你解答了, 04/11 12:17
outofyou: 但要先釐清初學者邏輯,不要又測%s、%p,又測&、*、**。 04/11 12:21
outofyou: 回答問題,*pp == name,所以這2個的%p及%s一樣才正常。 04/11 12:26