#include <iostream>
#include <string>
using namespace std;
// 方法1:传递了二维数组的首地址,和二维数组的行数
void print1_Array2D(int (*ptr)[4], int cnt)
{
for (int i = 0; i < cnt; i++)
{
for (int j = 0; j < 4; j++)
{
cout << ptr[i][j] << " ";
}
cout << endl;
}
}
// 方法2:传递了二维数组的首地址,和二维数组的行数
void print2_Array2D(int ptr[][4], int cnt)
{
for (int i = 0; i < cnt; i++)
{
for (int j = 0; j < 4; j++)
{
cout << ptr[i][j] << " ";
}
cout << endl;
}
}
// 方法3:传递了二维数组第一个元素的地址,和而为数组的行数和列数
void print3_Array2D(int *ptr, int row, int col)
{
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
cout << *(ptr + i * col + j) << " ";
}
cout << endl;
}
}
int main(int argc, char *argv[])
{
int iArray2D[3][4] =
{
{12, 3, 5 ,12},
{23, 4, 8 ,10},
{10, 7, 3 ,18}
};
print1_Array2D(iArray2D, 3);
cout << endl;
print2_Array2D(iArray2D, 3);
cout << endl;
print3_Array2D(&iArray2D[0][0], 3, 4);
return 0;
}
#include <iostream>
#include <string>
using namespace std;
// 方法4:将二维数组的每行地址传递给指针数组,及二维数组的行数和列数
void print4_Array2D(int* ptr[], int row, int col)
{
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
cout << *(ptr[i] + j) << " ";
}
cout << endl;
}
}
// 方法5:与方法4本质相同
void print5_Array2D(int** ptr, int row, int col)
{
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
cout << *(ptr[i] + j) << " ";
}
cout << endl;
}
}
int main(int argc, char *argv[])
{
int iArray2D[3][4] =
{
{12, 3, 5 ,12},
{23, 4, 8 ,10},
{10, 7, 3 ,18}
};
int* ptr[3] = {iArray2D[0], iArray2D[1], iArray2D[2]};
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 4; j++)
{
cout << *(ptr[i] + j) << " ";
}
cout << endl;
}
cout << endl;
print4_Array2D(ptr, 3, 4);
cout << endl;
print5_Array2D(ptr, 3, 4);
return 0;
}