雙重指標問題
各位朋友們好,最近我在研究雙重指標的用法,有個問題一直想不出來為什麼
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
推
04/10 23:57, , 1F
04/10 23:57, 1F
→
04/10 23:57, , 2F
04/10 23:57, 2F
→
04/10 23:58, , 3F
04/10 23:58, 3F
推
04/11 00:04, , 4F
04/11 00:04, 4F
推
04/11 05:30, , 5F
04/11 05:30, 5F
→
04/11 12:17, , 6F
04/11 12:17, 6F
→
04/11 12:21, , 7F
04/11 12:21, 7F
→
04/11 12:26, , 8F
04/11 12:26, 8F