| 
 
| 以下程序可以完美运行,但是稍微一修改就不行了(具体操作如下),请问这是为什么?谢谢。 #include "stdafx.h"
 #include <iostream>
 using namespace std;
 int main()
 {
 int (*p)[2]=new int[2][2];
 int i,j;
 int a[2][2]={1,2,3,4};
 for (i=0;i<2;i++)
 for (j=0;j<2;j++)
 {
 p[i][j]=a[i][j];
 }
 //p=a;
 cout<<"p[0][0]的地址为:"<<p[0]<<endl<<endl;
 for (i=0;i<2;i++)
 {
 for (j=0;j<2;j++)
 cout<<p[i][j]<<"\t";
 cout<<endl;
 }
 delete[] p;
 p=0;
 return 0;
 }
 但是修改一下源程序就不行了(不能删除p指针了),修改后的程序如下:
 #include "stdafx.h"
 #include <iostream>
 using namespace std;
 int main()
 {
 int (*p)[2]=new int[2][2];
 int i,j;
 int a[2][2]={1,2,3,4};
 /*for (i=0;i<2;i++)
 for (j=0;j<2;j++)
 {
 p[i][j]=a[i][j];
 }*/
 p=a;//使用该语句而注释掉以上两个for循环
 cout<<"p[0][0]的地址为:"<<p[0]<<endl<<endl;
 for (i=0;i<2;i++)
 {
 for (j=0;j<2;j++)
 cout<<p[i][j]<<"\t";
 cout<<endl;
 }
 delete[] p;//程序在这里会出错
 p=0;
 return 0;
 }
 | 
 |