C++入門經典-例6.12-使用數組地址將二維數組輸出
阿新 • • 發佈:2017-09-17
ace arr 指針運算 入門 none -1 n-1 使用數組 pac
1:以a[4][3]為例
a代表二維數組的地址,通過指針運算符可以獲取數組中的元素
(1)a+n代表第n行的首地址
(2)&a[0][0]既可以看作第0行0列的首地址,同樣也可以被看作是二維數組的首地址。&a[m][n]就是第m行n列元素的地址
(3)&a[0]是第0行的首地址,當然&a[n]就是第n行的首地址
(4)a[0]+(n-1)表示第0行第n個元素
(5)*(*(a+n)+m)表示第n行第m列
(6)*(a[n]+m)表示第n行第m列元素
2:代碼如下:
// 6.12.cpp : 定義控制臺應用程序的入口點。 // #include "stdafx.h" #includeView Code<iostream> using namespace std; void main() { int i,j; int a[4][3]={{1,2,3},{4,5,6},{7,8,9},{10,11,12}}; cout << "the array is: " << endl; for(i=0;i<4;i++) //行 { for(j=0;j<3;j++) //列 cout <<*(*(a+i)+j) << endl; } }
運行結果:
C++入門經典-例6.12-使用數組地址將二維數組輸出