Multi-dimensional arrays, pointer-to-pointer method

Hi guys!

So I get that a two-dimensional array, when created using a pointer-to-pointer method, is not laid out in memory contiguously, as in the case when the array is declared as int x[5][5];

As a result, we can't use the pointer arithmetic method for locating an element in a two dimensional array created via the pointer-to-pointer method. Yet, C++ allows us to write code that otherwise looks the same.

For example,

int **p_p_x;

p_p_x = new int*[3];

for (int i = 0; i < 3; i++)
{
p_p_x[i] = new int[3];
}

By specifying the 2d array as such, we are still able to reference an element by p_p_x[i][k]. Why is that?


Remember the operator precedence rules: p_p_x[i][k] is same as (p_p_x[i])[k]


Besides, you don't have to reserve rows separate blocks. You can reserve a single rows*cols block for data and set the pointers on the p_p_x array to point to appropriate elements in that block.
Topic archived. No new replies allowed.