int(*p)[2] = new int[3][2];
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 2; j++)
{
p[i][j] = i + j;
cout << p[i][j] << " ";
}
cout << endl;
}
int* p2 = (int*)p; //强转成一维数组
for (int i = 0; i < 6; i++)
{
cout << "p[" << i << "] = " << p2[i] << " ";
if ((i + 1) % 2 == 0 && i != 0)
{
cout << endl;
}
}
delete[]p; // p和p2指向的是同一块堆空间, 但是两个指针的地址是不一样的,还存在某种映射关系。但是释放只要释放p就行
cout << p << endl;
cout << p2 << endl;
p = NULL;
C++动态分配内存
最新推荐文章于 2024-07-22 09:38:37 发布