动态一维数组:
#include<iostream>
using namespace std;
int main()
{
int len;
cin>>len;
int *p=new int[len];
delete[] p;
p = NULL;
return 0;
}
动态二维数组:
int main()
{
int row, col;
cout << "Please enter the number for row and column: " << endl;
cin >> row >>col;
//为二维数组开辟空间
int **p = new int*[row];
for (int i = 0; i < row; i++)
{
p[i] = new int[col];
}
for (int j = 0;j < row; j++)
{
for (int k = 0; k < col; k++)
{
p[j][k] = (j + 1) * (k + 1);
cout << p[j][k] << &p[j][k];
}
cout<<endl;
}
//释放二维数组占用的空间
for (int m = 0; m < row; m++)
{
delete[] p[m];
}
delete[] p;
p = NULL;
return 0;
}