1.一维
函数内部
void main()
{
int a[3]={1,2,3};
int *b;
b= new int[3];
b[0]=*a;
*(b+1)=a[1];
b[2]=*(a+2);
delete []b;
}
函数外部
void newArray(int *&a,int *b){
a=new int[3];
a[0]=*b;
*(a+1)=b[1];
a[2]=*(b+2);
}
void deleteArray(int *&a){
delete []a;
}
void main(){
int a[3]={1,2,3};
int *b;
newArray(b,a);
deleteArray(b);
}
2.二维
函数内部
#include <stdio.h>
void main()
{
int a[][3]={
{1,2,3},
{4,5,6},
{7,8,9}
};
int (*p)[3]=new int[3][3]; //数据组指针
int *q[3]; //指针数组
q[0]=a[0];
q[1]=a[1];
q[2]=a[2];
// p=a;
for (int i=0;i<3;i++)
{
for(int j=0;j<3;j++){
p[i][j]=a[i][j];
}
}
printf("
%d ",sizeof(a)); //36
printf(" %d " ,sizeof(p)); //4
printf(" %d
" ,sizeof(*p)); //12
delete []p;
}
函数外部
void newArray( int (*&p)[3], int a[][3]){
p=new int[3][3];
for (int i=0;i<3;i++)
{
for(int j=0;j<3;j++){
p[i][j]=a[i][j];
}
}
}
void deleteArray( int (*&p)[3]){
delete []p;
}
void main()
{
int a[][3]={
{1,2,3},
{4,5,6},
{7,8,9}
};
int (*p)[3];
newArray(p,a);
deleteArray(p);
}
3.三维
函数内部
void main()
{
int (*p)[3][2] =new int[4][3][2];
for (int i=0;i<4;i++)
{
for (int j=0;j<3;j++)
{
for(int k=0;k<2;k++){
p[i][j][k]=i+j+k;
printf(" %d ",i+j+k);
}
printf("
");
}
printf("
");
}
delete []p;
}
函数外部
void newArray( int (*&p)[3][2]){
p=new int[4][3][2];
for (int i=0;i<4;i++)
{
for (int j=0;j<3;j++)
{
for(int k=0;k<2;k++){
p[i][j][k]=i+j+k;
printf(" %d ",i+j+k);
}
printf("
");
}
printf("
");
}
}
void deleteArray( int (*&p)[3][2]){
delete []p;
}
void main()
{
int (*p)[3][2] ;
newArray(p);
deleteArray(p);
}