作者enonrick (EnonRick)
看板C_and_CPP
标题Re: [问题] 2D array的address
时间Thu Apr 26 13:59:31 2018
简单理解:
*、[] 在宣告外是 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://webptt.com/cn.aspx?n=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