Friday, 15 July 2011

c++ - Put a multidimensional array into a one-dimensional array -



c++ - Put a multidimensional array into a one-dimensional array -

i've got question. i'm writing simple application in c++ , have next problem: want utilize two-dimensional array specify position of object (x , y coordinates). when created such array, got many access violation problems, when accessed it. i'm not pretty sure, violations came from, think, stack not big plenty , shuld utilize pointers. when searched solution utilize multidimensional array in heap , point on it, solutions complicated me.

so remembered there's way utilize "normal" one-dimensional array multidimensional array. not remember exactly, how can access right way. declared way:

char array [screen_height * screen_width];

then tried fill way:

for(int y = 0; y < screen_height; y++) { for(int x = 0; x < screen_width; x++) { array [y + x * y] = ' '; } }

but not right, because char @ position y + x * y not specified (because y + y * x points same position) pretty sure, there way this. maybe wrong, tell me :d in case, solution utilize multidimensional array great!

you don't want y + x*y, want y * screen_width + x. said, 2d array declared as:

char array[screen_height][screen_width];

has same memory layout, , access straight way want:

array[y][x] = ' ';

c++ arrays multidimensional-array

No comments:

Post a Comment