Array of Tiles [Archive] - C Board

PDA

View Full Version : Array of Tiles


Okiesmokie
05-28-2002, 06:34 PM
How can i make an array of tiles? i have made a struct that looks something like this:

typedef struct _TILE {
bool blocked;
UINT graphic;
} TILE

and i want to make an array of this, i have tried:
TILE tTiles[10][10] but this doesnt seem to work, can someone please help me?

Thanks

[EDIT]
The initial thing works, but when i try to go:

int x=1;
tTiles[x].blocked = true;
doesnt work

jdinger
05-28-2002, 07:07 PM
From what you've posted here the reason it's not working would be because you're trying to access a 2d array as if it were a 1d array.

TILE tTiles[10][10] but this doesnt seem to work, can someone please help me?

Thanks

[EDIT]
The initial thing works, but when i try to go:


code:--------------------------------------------------------------------------------int x=1;
tTiles[x].blocked = true;


your code should look more like:

//for clarity I defined x, etc.
int x=0;
int y=2;

TILES tTiles[10][10];

tTiles[x][y].blocked=true;


Unless leaving out the other dimension was a typo, then I'd have to see more of your code and errors to know exactly what part isn't working.

Okiesmokie
05-28-2002, 07:11 PM
ahh nope it wasnt a typo, but i was taught that you can access a 2D array like a 1D array, ie:

int array[2][2] would be equal to int array[4], ill try it and reply to tell if it works

Da-Spit
05-29-2002, 12:36 AM
I don't think that would work,
since you could have [1][3] or [3][1], etc...

Uraldor
05-29-2002, 08:36 PM
it does work you just have to make sure you index into the array properly.

Edit: why you'd want to do it that way fora simple program, i dont know!

SilentStrike
05-29-2002, 10:14 PM
"it does work you just have to make sure you index into the array properly."

It would work if you casted it.

tiles[x] is a TILE*, not a TILE

warning, I really don't advise this in real program it's just for the sake of understanding how 2d arrays work.


#include <iostream>

struct ugly {
int number;
};

int main() {
ugly mess[10][10];
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
mess[i][j].number = i * j;
}
}

// std::cout << mess[2].num << std::endl; // illegal
std::cout << mess[1][5].number << std::endl; // 1*10 + 5 th = 15 th spot = 1*5
std::cout << ((ugly*) mess)[15].number << std::endl; // same thing, uglier

return 0;
}