楼上两位,回答给得迟了些,望见谅,
下面是这个问题的释义性程序,千言万语不如一段实际的代码。
#include <iostream>
using namespace std;
int main()
{
int i = 0, j = 0, k = 0;
// manipulate data with two dimensional array through pointer
cout<<"manipulate data with two dimensional array through pointer"<<endl;
int a [2][3] = {
{1,2,3},
{4,5,6}
};
int * p = a[0];
for(i = 0; i<2; i++)
{
for(j = 0; j<3; j++)
{
cout<<*((p+i)+j)<<" ";
}
cout<<endl;
}
cout<<endl;
// create data with new without dimension
cout<<"create data with new without dimension"<<endl;
int * pI = new int;
*pI = 3;
cout<<*pI<<endl;
delete pI;
cout<<endl;
// create data with new in one dimension
cout<<"create data with new in one dimension"<<endl;
int * pOne = new int[3];
for(i = 0; i<3; i++)
{
*(pOne+i) = i+1;
cout<<*(pOne+i)<<" ";
}
delete [] pOne;
cout<<endl<<endl;
// create data with new in two dimension
cout<<"create data with new in two dimension"<<endl;
int (* pTwo)[3] = new int[2][3];
for(i = 0; i<2; i++)
{
for(j = 0; j<3; j++)
{
*(*(pTwo+i)+j) = (i+1)*(j+1);
//pTwo[i][j] = (i+1)*(j+1);
cout<<*(*(pTwo+i)+j)<<" ";
}
cout<<endl;
}
delete [] pTwo;
cout<<endl;
// create data with new in multidimension
cout<<"create data with new in multidimension"<<endl;
int (* pMulti)[3][4] = new int[2][3][4];
for(i = 0; i<2; i++)
{
for(j = 0; j<3; j++)
{
for(k = 0; k<4; k++)
{
*(*(*(pMulti+i)+j)+k) = (i+1)*(j+1)*(k+1);
cout<<*(*(*(pMulti+i)+j)+k)<<" ";
}
cout<<endl;
}
cout<<endl;
}
delete [] pMulti;
return 0;
}