Re: [問題] 2D array的address
簡單理解:
*、[] 在宣告外是 de-reference
de-reference 後
是 primitive type -> 取值
不是 primitive type -> 參照(reference) -> 意即當下的 address
int b[2][3];
要達到 primitive type 需要做到兩個 de-reference
------------------------------------------------
以下
int a[3][3] = {
{1,2,3},
{4,5,6},
{7,8,9},
};
記憶體裡會長這樣
0x0000 {0x0000 {1, 2, 3}, 0x000c {4, 5, 6}, 0x0018 {7, 8, 9}} int(*)[3][3]
[0]: 0x0000 {1, 2, 3} int(*)[3]
[0]: 0x0000 : 1 int*
[1]: 0x0004 : 2
[2]: 0x0008 : 3
[1]: 0x000c {4, 5, 6}
[0]: 0x000c : 4
[1]: 0x0010 : 5
[2]: 0x0014 : 6
[2]: 0x0018 {7, 8, 9}
[0]: 0x0018 : 7
[1]: 0x001c : 8
[2]: 0x0020 : 9
注意:藍色部份"位址一樣" 但 "型別不同"
printf("%x %x %x",a,*a,**a);
a 是 int(*)[3] 位址於 0x0 -> 值為0x0 (reference)
*a 是 int* 位址於 0x0 -> 值為0x0 (reference)
**a 是 int 位址於 0x0 已達 primitive type,取值 1
a[0] = 0
*a = *(a+0) = *(a) = *(&a[0]) = a[0] = 0
同樣要取值6 都要兩次 de-reference
1.a[1][2]
2.*(*(a+1)+2)
3.(*(a+1))[2]
4.*(a[1]+2)
※ 引述《zzss2003 (brotherD)》之銘言:
: 圖片: https://imgur.com/a/8Q7d3GH
: 在這個影集當中,我不懂為什麼*B or B[0]是400,不是應該是2嗎?
: &B[0][0]是400我能理解,但沒辦法理解*B與B[0],影片中也沒提到為什麼
: 自己用了GCC,compile後也是同樣結果
: 能請前輩們提供一下線索嗎?_?
: 謝謝
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 125.227.143.169
※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1524722373.A.884.html
※ 編輯: enonrick (125.227.143.169), 04/26/2018 14:05:05
※ 編輯: enonrick (125.227.143.169), 04/26/2018 16:21:45
討論串 (同標題文章)
完整討論串 (本文為第 5 之 5 篇):