在C++中定义二维数组有很多种方式
1.第一种方式:定义一个二级指针(指向指针的指针)
#include <iostream>
using namespace std;
void Traverse(int** p);
int main() {
// two-dimension array
int m = 3, n = 5;
int** arr = new int*[m];
arr[0] = new int[n];
arr[1] = new int[n];
arr[2] = new int[n];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
arr[i][j] = 10;
}
}
Traverse(arr);
for (int i= 0; i < 3; i++) {
delete[] arr[i];
arr[i] = nullptr;
}
delete[] arr;
arr = nullptr;
return 0;
}
void Traverse(int** p) {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 5; j++) {
cout << p[i][j] << " ";
}
cout << endl;
}
}
标签:arr,一个二维,int,Traverse,C++,++,数组,new
From: https://www.cnblogs.com/gjsun/p/17578544.html