c++ - Traversing a multi array with pointers -
for ( j = 0; j < d1; j++ ){ m += j; ( = 0; < d1*d2; +=d2){ cout << *(m+i); } cout << endl; }
d1,d2 array dimensions
and
int* m = new int [d1*d2];
i want traverse on array , group , print columns.can't figure what's wrong code.seems working fine untill 3rd iteration in following example:
let's input values 1 2 3 4 5 6 7 8 9
i get:
1 4 7 2 5 8 4 7 (something random)
in
m += j;
you first incrementing m
0, one, 2. if took copy
int *start = m;
then in first iteration of outer loop, we'd have
m == start
in second,
m == start + 1
in third
m == start + 3
you'd want m == start + 2
there. except want keep m
in order delete
@ end, shouldn't change m
@ use like
for ( j = 0; j < d2; j++ ){ ( = j; < d1*d2; +=d2){ cout << *(m+i); } cout << endl; }
Comments
Post a Comment