第一种:
int (*p)[5] = new int[COL][5]; //已知列数
for (int i = 0; i < 5; i++)
{
(*p)[i] = 0 + i;
(*(p + 1))[i] = 1 + i;
(*(p + 2))[i] = 2 + i;
}
std::cout << p << std::endl;
std::cout << (*p)[0]<<std::endl;
p = p + 1;
std::cout << *(*p + 1);
std::cout << *((*p + 1) + 1);
p = p - 1; //释放之前p应该为首地址,这样才能全部释放内存
delete []p;
p = NULL;
第二种:不连续申请内存
int **p = new int*[3];
for (int i = 0; i < 3; i++)
{
p[i] = new int[5]; //按行申请内存,行内连续,行与行不一定连续
}
for (int i = 0; i < 3; i++)
{
delete []p[i]; //按行释放内存
}
delete[]p; //释放指针数组