hello.
i can do that
int i=5;
int p=new int [i];
i want to do that
int i=5,j=9;
int p=new int [i][j];
how can i do it?
thanks.
hello.
i can do that
int i=5;
int p=new int [i];
i want to do that
int i=5,j=9;
int p=new int [i][j];
how can i do it?
thanks.
> int i=5;
> int p=new int [i];
Should be
int *p = new int[i];
> int i=5,j=9;
> int p=new int [i][j];
Should be
int **p = new int*[i];
for ( int r = 0 ; r < i ; r++ ) p[i] = new int[j];
Basically, you allocate one level of indirection at a time.
However, if 'j' was constant in your example, then you could do this
int i=5; const int j=9;
int (*p)[j] = new int[i][j];
Either way, you can use p[x][y] to access individual elements.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
Code:int **p; int x = 5; int y = 9; // Allocate it p = new int*[x]; for (int i = 0; i < x; i++) ptr[i]=new int[y]; //... Do something with it // Delete it for(int j = 0; j < x; j++) delete [] ptr[j]; delete ptr;