如何理解:
数组名其实是首元素的指针。
以m*n矩阵转置函数为例:
#include <iostream>
#include <stdio.h>
using namespace std;
const int m = 3;
const int n = 2;
typedef int(*R)[m];
R TransMatrix(const int a[][n] , int b [][m]) {
for (size_t i = 0; i < m; i++) {
for (size_t j = 0; j < n; j++)
{
b[j][i] = a[i][j];
}
}
return b;
}
void ShowMatrix(int* a , const int irows, const int icols) {
for (size_t i = 0; i < irows; i++) {
for (size_t j = 0; j < icols; j++)
{
cout <<*(a+i*icols+j) << " ";
}
cout << endl;
}
}
int main()
{
int A[m][n];
int B[n][m];
cout << "请输入数组" << endl;
for (size_t i = 0; i < m; i++) {
for (size_t j = 0; j < n; j++)
{
cin >> A[i][j];
}
}
cout << "----------输入的数组是:-----------" << endl;
ShowMatrix((int*)A,m,n);
cout << "----------转置是:-----------" << endl;
ShowMatrix((int*)TransMatrix(A,B), n, m);
return 0;
}