分配二維數組:
int **ptr=new int*[n1];
for(int i=0;i<n1;i++)
ptr[i]=new int[n2];
二維數組清除:
for(int i=0;i<n1;i++)
delete []ptr[i];
delete []ptr;
動態分配清除三維數組
#include<iostream>
using namespace std;
int main()
{
//system("cp testdouble.c hehe ");
int i,j,k;
int r,s,t;
cout<<"Please Enter the 1st dimension:";
cin>>i;
cout<<"Please Enter the 2nd dimension:";
cin>>j;
cout<<"Please Enter the 3rd dimension:";
cin>>k;
cout<<i<<'/t'<<j<<'/t'<<k<<endl;
int ***p=new int**[i];
for(t=0;t<i;t++)
p[t]=new int*[j];
for(t=0;t<i;t++)
for(s=0;s<j;s++)
p[t][s]=new int[k];
for(r=0;r<i;r++)
for(s=0;s<j;s++)
for(t=0;t<k;t++)
p[r][s][t]=r*j*k+s*k+t;
for(r=0;r<i;r++)
{
cout<<endl;
for(s=0;s<j;s++)
{
cout<<endl;
for(t=0;t<k;t++)
cout<<p[r][s][t]<<'/t';
}
}
cout<<endl;
for(r=0;r<i;r++)
for(s=0;s<j;s++)
delete []p[r][s];
for(r=0;r<i;r++)
delete []p[r];
delete []p;
return 0;
}