Thread: strange array issue

  1. #1
    Registered User
    Join Date
    May 2007
    Posts
    3

    strange array issue

    I have a class that contains coordinates for a square.
    I set the values, and then right afterwards, I try to access them ,and every index is the same as the last one!
    Anyone know why this is?
    Code:
    	tile block[3][3];	
    	for (p=0;p<3;p++)
    	{
    		for (i=0;i<3;i++)
    		{
    			block[i][p].setX(curX);
    			block[i][p].setY(curY);
    			block[i][p].setWidth(TWidth);
    			block[i][p].setHeight(THeight);
    			printf("%d %d %d %d \n", block[0][0].getX(),block[0][0].getY(),block[0][0].getWidth(),block[0][0].getHeight());
    			curX = curX+TWidth+20;
    		}
    		curX=50;
    		curY= curY+THeight+10;
    	}
    	printf("%f %f %f",block[0][0].getRf(),block[0][0].getGf(),block[0][0].getBf());
    	
    	
    	for (p=0;p<3;p++)
    	{
    		for (i=0;i<3;i++)
    		{
    
    			printf("%d %d %d %d \n", block[i][p].getX(),block[i][p].getY(),block[i][p].getWidth(),block[i][p].getHeight());
    			printf("\n");
    		}
    
    	}
    and the output
    50 50 50 50
    120 50 50 50
    190 50 50 50
    50 110 50 50
    120 110 50 50
    190 110 50 50
    50 170 50 50
    120 170 50 50
    190 170 50 50
    1.000000 0.000000 0.000000190 170 50 50

    **
    *******Right here is where it starts outputting the numbers again after the loop
    **
    190 170 50 50

    190 170 50 50

    190 170 50 50

    190 170 50 50

    190 170 50 50

    190 170 50 50

    190 170 50 50

    190 170 50 50

  2. #2
    Registered User
    Join Date
    Dec 2006
    Location
    Scranton, Pa
    Posts
    252
    Perhaps you should change the ints p & i to another assigned letter in your second for loop. Does it reset p & i back to zero?

  3. #3
    Registered User
    Join Date
    May 2007
    Posts
    3
    yeah they reset to zero in the for loop initialization

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    I would think it would be the opposite. The printf in the first for loop only accesses element 0,0, the second loop looks fine.

  5. #5
    Registered User
    Join Date
    May 2007
    Posts
    3
    look at the Output, tile[0][0] is different every time :S

    and For( int whatever=0;**;i++) resets back to zero

  6. #6
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    You don't happen to be declaring the X, Y, Width, and Height members of the tile class as "static," are you?

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > printf("&#37;d %d %d %d \n", block[0][0].getX()
    How come you're getting changing numbers, when you keep printing the [0][0] element?

    I think your class is doing something else. Post the class and those member functions as well.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Modify an single passed array element
    By swgh in forum C Programming
    Replies: 3
    Last Post: 08-04-2007, 08:58 AM
  2. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  3. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  4. two dimensional dynamic array?
    By ichijoji in forum C++ Programming
    Replies: 6
    Last Post: 04-14-2003, 04:27 PM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM