Thread: Array of Tiles

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    387

    Angry 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:
    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;
    doesnt work

  2. #2
    Used Registerer jdinger's Avatar
    Join Date
    Feb 2002
    Posts
    1,065
    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:
    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.

  3. #3
    Registered User
    Join Date
    Jan 2002
    Posts
    387
    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
    "There are three kinds of people in the world...
    Those that can count and those that can't."

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    99

    Huh?

    I don't think that would work,
    since you could have [1][3] or [3][1], etc...

  5. #5
    Registered User
    Join Date
    Dec 2001
    Posts
    421
    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!
    Quidquid latine dictum sit, altum sonatur.
    Whatever is said in Latin sounds profound.

  6. #6
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    "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;
    }
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 10:33 AM
  3. Replies: 6
    Last Post: 11-09-2006, 03:28 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM