I''m pretty sure what Muzza was talking about would be:
int max_x, max_y, x, y;// do stuff to figure out what above values should beint *array = malloc( sizeof(int) * max_x * max_y );// then to access array member array[x][y]array[(x * max_y) + y] = whatever;
Frankly Galdiator''s way is much neater and much more typical, but the above is the one of the old C ways of doing things.
std::vector< std::vector<int> > vec2d(10);//resize all sub-vectors to 10for(int i = 0; i< 10; ++i){ vec2d<i>.resize(10);}//use an element in the vectorvec2d[2][5] = 7;
Vectors? hmm...any resources on how to use them? As for...
#include int main() { int **myArray; int x, y; x = 10; y = 15; myArray = new int*[x]; for (int i = 0; i < x; i++) myArray = new int[y]; myArray[0][0] = 12; printf("%d", myArray[0][0]); for (i = 0; i < x; i++) delete [] myArray;<br> delete [] myArray;<br> return 0;<br>}<br><br>on line 9, how come you don''t have to "deference" the myArray pointer with the "*" operation to get the value instead the address of?<br><br> </i>
I wanted to avoid creating a temporary vector when make a 10 by 10 array, but since your probably not going to be creating many multi-dimensional arrays the speed & memory difference wont matter. So you might as well use the nicer construct-from-temporary method:
std::vector< std::vector<int> > vec2d(10, vector<int>(10));//access an element in the vectorvec2d[2][5] = 7;
If it does end up being slow, you can always speed it up latter.
vbisme: the reason you don't have to deference the vector is the elements are stored by value and the index operator returns a reference. e.g.
std::vector<int> v1(10); // a vector of 10 int'sv1[0] = 1; // this will return int&, which gets assigned to 1;std::vector<int*> v2(10); // a vector of 10 int pointersv2[0] = new int; // this returns a int*& which gets assigned to a new int.*v2[0] = 1; // this deferences the pointer in element 0 and assigns it to 1
For more info on std::vector have a look at http://www.sgi.com/Technology/STL/Vector.html