Array of Tiles

• 05-28-2002
Okiesmokie
Array of Tiles
How can i make an array of tiles? i have made a struct that looks something like this:

Code:

```typedef struct _TILE {     bool blocked;     UINT graphic; } TILE```
and i want to make an array of this, i have tried:

Thanks

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

Code:

```int x=1;     tTiles[x].blocked = true;```
doesnt work
• 05-28-2002
jdinger
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.

Quote:

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:
Code:

```//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.
• 05-28-2002
Okiesmokie
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
• 05-29-2002
Da-Spit
Huh?
I don't think that would work,
since you could have [1][3] or [3][1], etc...
• 05-29-2002
Uraldor
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!
• 05-29-2002
SilentStrike
"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.

Code:

```#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; }```