一维数组寻址
数组名表示[数组第0个元素]的[地址][常量]
type array[M]
array[N] address is
(int)array + sizeof(type) * N
数组长度
sizeof(array) / sizeof(array[0])
二维数组寻址
type array[N][M]
type array[x][y] address is:
(int)array + sizeof(type[M]) * x +sizeof(type) * y
指针寻址
array是表示数组第0个元素地址的指针常量,指针类型为元素类型
type array[M];
array[n] <==> *(type *)((int)array +sizeof(type)*n);
type *ptr = xxxx;
type *ptrTest = xxxx;
int n = xxxx;
(ptr + n) is (int)ptr + sizeof(type)*n;
ptr + n <==> (type * const)((int)ptr +sizeof(type)*n);
ptr - ptrTest <==> ((int)ptr - (int)ptrTest) /sizeof(type);
array[n] == *(array + n);
&array[n] == array + n;
type *ptr = array;
type[n] == array[n];
ptr++; //OK
array++; //ERROR, array is pointer const
本文详细探讨了一维数组和二维数组的寻址方式,以及指针在数组操作中的应用。介绍了如何通过数组名获取首元素地址,并计算数组长度。同时,解释了指针加减法以及指针与数组的关系,如`array[n]`与`*(array+n)`等价。
2005

被折叠的 条评论
为什么被折叠?



