//C++中并没有多维数组的概念,实际上是数组的数组
//二维数组作为参数传入的三种方法
#include<iostream>
using namespace std;
//需要指明每一行有多少个元素
void Print1(int M[][3],int n)
{
for (int i = 0;i < n;i++)
{
for (int j = 0;j < 3;j++)
cout << M[i][j] << "\t";
cout << endl;
}
}
//使用指向数组的指针
void Print2(int(*M)[3], int n)
{
for (int i = 0;i < n;i++)
{
for (int j = 0;j < 3;j++)
cout << M[i][j] << "\t";
cout << endl;
}
}
//使用指针
void Print3(int*M, int row, int col)
{
for (int i = 0;i < row;i++)
{
for (int j = 0;j < col;j++)
cout << *(M + i*col + j) << "\t";
cout << endl;
}
}
int main()
{
int M[2][3] = { 1,2,3,4,5,6 };
int L[][3] = { 1,2,3,4,5,6 };
for (int i = 0;i < 2;i++)
{
cout << "地址:" << M[i] << endl;
for (int j = 0;j < 3;j++)
cout << *(*(M+i)+j)<<*(M+i*3+j) << "\t";
cout << endl;
}
Print1(M, 2);
Print2(M, 2);
Print3((int*)M, 2, 3);
cout << M << endl;
cout << &M << endl;
system("pause");
}
C++二维数组传参
最新推荐文章于 2024-04-17 23:46:59 发布