Thread: Why doesn't this work - about tables

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    9

    Why doesn't this work - about tables

    Hi, I'm writing a small game, now figuring how to set up the game board. This code creates an area in which the game will be played.. Now, when I change the X or Y it shows no error, though when I run it an exception is generated. I guess it's something to do with referring outside my table, but can't see it.

    So, here's the code:
    Code:
    #include <stdio.h>
    #define X 20
    #define Y 20
    
    
    int main (void)
    {
    	char table [X][Y] = {' '};
    	int i, j;
    	
    	for (i=0;i<Y;i++)
    	{
    		table[0][i] = 'O';
    	}
    	for (i=0;i<Y;i++)
    	{
    		table[Y-1][i] = 'O';
    	}
    	for (i=0;i<X;i++)
    	{
    		table[i][0] = 'O';
    	}
    	for (i=0;i<X;i++)
    	{
    		table[i][X-1] = 'O';
    	}
    
    
    	for (i=0;i<Y;i++)
    	{
    		for (j=0;j<X;j++)
    		{
    			printf("%c", table[i][j]);
    		}
    		printf("\n");
    	}
    
    	return 0;
    }

  2. #2
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    the tables are constructed:

    int table[Y][X];

    if I'm not mistaken. Not table[X][Y]. Was it when you changed them so that they're different? It's a bounds error then.

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    9
    Ok, so the first array is for vertical movement, the other for horizontal? Ok, thanks a lot.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. getline() don't want to work anymore...
    By mikahell in forum C++ Programming
    Replies: 7
    Last Post: 07-31-2006, 10:50 AM
  2. Why don't the tutorials on this site work on my computer?
    By jsrig88 in forum C++ Programming
    Replies: 3
    Last Post: 05-15-2006, 10:39 PM
  3. Problems in getting OpenGL to work
    By zonf in forum C Programming
    Replies: 5
    Last Post: 02-13-2006, 04:48 AM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM
  5. DLL __cdecl doesnt seem to work?
    By Xei in forum C++ Programming
    Replies: 6
    Last Post: 08-21-2002, 04:36 PM