2d matrix allocation

please tell how to dynamically allocate a 2-d array usning new operator
For example

int n = 20;
int ( *p )[10] = new int[n][10];
how do i enter elements in this matrix ? suppose i have to such matrices ,how do i add them ?
1
2
3
4
5
6
7
for ( int i = 0; i < n; i++ )
{
   for ( int j = 0; j < 10; j++ )
   {
      p[i][j] = n * i + j;
   }
}
can u explain the line
int ( *p )[10] = new int[n][10];
When an array is being allocated then the left side of a new expression shall have type pointer to an element of the array. For example

int *p = new int[10];

Here element of the array has type int. So in the left side of the expression there is pointer to int that is int *.

For multidimensional arrays of dimension N their elements are arrays of dimension N - 1.

So for two dimensional array there shall be a pointer to one dimensional array in the left side of a new expression. The record int ( *p )[10] defines a pointer to a one dimensional array of 10 elements.
Topic archived. No new replies allowed.