I know how to create a multidumentional array statndard way:
const int m = 12;
const int y = 3;
int sales[y][n];
And I know how to create a pointer that points to one dimentional array:
int * ms = new int[m];
But is it possible to create a pointer that points to multidumentional array?
int * sales = new int[y][m]; // doesn't work
int * mSales = new int[m]; // ok
int * ySales = new int[y]; // ok
mSales * ySales = new mSales[y]; // doesn't work, mSales is not a type
How to create such a pointer?
The expression
new int[m][n]creates anarray[m] of array[n] of int.Since it’s an array new, the return type is converted to a pointer to
the first element:
pointer to array[n] of int. Which is what you haveto use:
Of course, you really shouldn’t use array new at all. The
_best_solution here is to write a simple
Matrixclass, usingstd::vectorfor the memory. Depending on your feelings on the matter,you can either overload
operator()( int i, int j )and use(i, j)for indexing, or you can overload
operator[]( int i )to return ahelper which defines
operator[]to do the second indexation. (Hint:operator[]is defined onint*; if you don’t want to bother withbounds checking, etc.,
int*will do the job as the proxy.)Alternatively, something like:
will do the job, but in the long term, the
Matrixclass will be worthit.