#include <cstdlib>
#include <iostream>
using namespace std;
void New2DArray(int ** &pArray, int row, int col)
{
pArray = new int*[row];
int i, j;
for(i = 0; i < row; i++)
{
pArray[i] = new int[col];
}
/*
// 测试用,也要以用于初始化
for(i = 0; i < row; i++)
{
for(j = 0; j < col; j++)
{
pArray[i][j] = i*col + j;
cout<<"("<<i<<' '<<j<<") "<<pArray[i][j]<<' ';
}
cout<<endl;
}
*/
}
void Delete2DArray(int ** &pArray, int row)
{
for(int i = 0; i < row; i++)
delete[] pArray[i];
delete[] pArray;
pArray = NULL;
}
int main(int argc, char *argv[])
{
int **pArray = NULL;
New2DArray(pArray, 4, 5);
Delete2DArray(pArray, 4);
system("PAUSE");
return EXIT_SUCCESS;
}
以上代码已在Dev-C++和VC++.net上通过测试
输出结果如下:
(0 0) 0 (0 1) 1 (0 2) 2 (0 3) 3 (0 4) 4
(1 0) 5 (1 1) 6 (1 2) 7 (1 3) 8 (1 4) 9
(2 0) 10 (2 1) 11 (2 2) 12 (2 3) 13 (2 4) 14
(3 0) 15 (3 1) 16 (3 2) 17 (3 3) 18 (3 4) 19
请按任意键继续 . . .