声明连续的二维数组指针 [英] Declaring contiguous 2D array pointer
本文介绍了声明连续的二维数组指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经学会了使用指针来声明动态二维数组.但是我被告知这不会创建一个连续的二维数组.
I have learnt to declare a dynamic 2D array using a pointer as such. However I was told this does not create a contiguous 2D array.
int **p;
p = new int*[M];
for (int i = 0; i < M; ++i) {
p[i] = new int[N]; }
修改代码以创建指向连续二维数组的动态指针的方法是什么?
What is the way to modify the code to create a dynamic pointer to a contiguous 2D array?
推荐答案
试试这个
int **p= new int*[rows];
int size= rows*cols;
p[0]= new int[size];
for(int i= 1; i < rows; i++) {
p[i]= &p[0][i*cols];
}
这篇关于声明连续的二维数组指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文