关于二维数组在内存中的排列
当我们声明一个数组
如:nt arr[2][2] = {{1,2},{3,4}};
我们假设该数组的起始地址为0,那么直接输出arr的地址便为0,且范围是0~7,占用8个字节。然而有趣的是arr+1的地址并非为4,而是为8。因此,想要输出行之中列的地址,需要在arr的地址上再进行操作,即 (*arr+0)+1,这便得到了数组中第一行第二列数值的地址;
输出分为以下
(1)cout << arr ;将会输出首位的地址;
(2)cout << arr [x];将会输出第x行的地址;
(3)cout << arr[x][y]; 将会输出第 x 行,第 y 列的数值
(4)cout << *(arr+0);将会输出首行首位的数值
(5)cout << *(*(arr+0)+1);将会输出第一行第二列的数值;
(6)整体输出可以用循环将数组输出
#includeusing namespace std; int main() { int arr[3][3]={{1,2,3},{4,5,6},{7,8,9}}; int i,j; for(i=0;i<=2;i++) { for(j=0;j<=2;j++) { cout << arr[i][j] << " " ; } cout << endl; } return 0; }
同样的,使用地址将其输出
#includeusing namespace std; int main() { int arr[3][3]={{12,22,32},{42,52,62},{72,82,92}}; int i,j; for(i=0;i<=2;i++) { for(j=0;j<=2;j++) { cout << *(*(arr+i)+j) << " " ; } cout << endl; } return 0; }
新手上路,若有错误,还请大佬斧正



