so I'm trying out some used in malloc and generally understand how to use it for 1d arrays, but having trouble with 2d arrays.
So I just want to to create a 2d array with all 1's. Would also be great to differentiate the first array from the second. I.e. first array has 3 second has 10;
Thanks!!
Code:#include <iostream> #include <vector> #include <algorithm> using namespace std; int main() { int ** test; int size = 3; test = (int**) malloc (size*sizeof(int)); for(int o = 0; o<size; o++) { for(int i=0;i<size;i++) { test[o][i] = 1; cout << test[o][i]; } } return 0; }



3Likes
LinkBack URL
About LinkBacks




I don't see an implication that the method is less elegant. I think that needing to reallocate individual rows is a possibility that cannot always be dismissed (even if we're not talking about a "jagged" array of arrays), I don't see what's so scary about "explicit pointer arithmetic" compared to the scary "multi-dimensional iterator" 