C++ Pointer to 2D Dynamic Array? -


i bit of c++ newbie , working on project , little stuck. need create dynamic 2d ragged array have pointer point it. here have:

int ** x = new int*[3]; int *** y = ???; 

now, after do:

x[n] = new int[length]; x[++n] = new int[length2]; //etc etc 

i can access values in array through statement like:

int num = x[i][j]; 

what want able same array values through y like:

int num2 = *y[i][j]; 

so, in case, num2 , num should have same value, how go allocating memory y , assigning it?

thanks!

here example of creating 2d array in c++.

#include <iostream>  int main() {   // dimensions   int n = 3;   int m = 3;    // dynamic allocation   int** ary = new int*[n];   for(int = 0; < n; ++i)       ary[i] = new int[m];    // fill   for(int = 0; < n; ++i)     for(int j = 0; j < m; ++j)       ary[i][j] = i;    // print   for(int = 0; < n; ++i)     for(int j = 0; j < m; ++j)       std::cout << ary[i][j] << "\n";    // free   for(int = 0; < n; ++i)     delete(ary[i]);   delete [] ary;    return 0; }  // output 0 0 0 1 1 1 2 2 2 

check this answer more.

so now, have created 2d array. want pointer point it. not need allocate memory again (this wrong)!

so, need this:

  int ***p = &ary;    // print   for(int = 0; < n; ++i)     for(int j = 0; j < m; ++j)       std::cout << (*p)[i][j] << "\n"; 

...but wait, it's c++! should use std::array or std::vector instead of primitive arrays (expanding them 2d, std::vector< std::vector >)!


Comments

Popular posts from this blog

javascript - how to protect a flash video from refresh? -

visual studio 2010 - Connect to informix database windows form application -

android - Associate same looper with different threads -