unwanted Variable Change

This is a discussion on unwanted Variable Change within the C Programming forums, part of the General Programming Boards category; I have a struct: Code: typedef struct PLAYER { char *name; int cash; int AI; card hand[1]; } player; and ...

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    4

    Question unwanted Variable Change

    I have a struct:
    Code:
    typedef struct PLAYER
    {
    	char *name;
    	int cash;
    	int AI;
    	card hand[1];
    } player;
    and another:
    Code:
    typedef struct CARD
    {
    	short int index; 
    	Suit suit; 
    	Value value;
    	char name[18];
    } card;
    within this section of code sumthing odd happens:

    Code:
    	extern player Player[];
    	initPlayers(Name);
    	printf( "CashB = %d\n", Player[0].cash ); /* Player[0].cash = 2500 */
    	newDeck(Deck);
    	printf( "CashC = %d\n", Player[0].cash ); /* Player[0].cash = 3 */
    newDeck function. in Deck.c:
    Code:
    int newDeck(card * Deck)
    {
    	int i;
    	extern player Player[];
    	printf( "CashBA = %d\n", Player[0].cash ); /* Player[0].cash = 2500 */
    	for( i=0; i<52; i++)
    	{
    		newCard(&Deck[i], i);
    	}
    	printf( "CashBB = %d\n", Player[0].cash ); /* Player[0].cash = 3 */
    	return 0;
    }
    newCard function. in Card.c:
    Code:
    int newCard(card *Card, int index)
    {
    	extern player Player[];
    	printf( "CashBAA = %d\n", Player[0].cash ); /* Player[0].cash = 2500 */
    	Card->index = index;
    	Card->value =  (index%13);
    	Card->suit =  (index/13);
    	cardName(Card);
    	printf( "CashBAB = %d\n", Player[0].cash ); /* 	if(index == 51)Player[0].cash = 3; 
    							else 
    							Player[0].cash = 3;*/
    }
    Does anyone know why Player[0].cash could change, when no Player variables are even mentioned or used within this function newCard (i only used 'extern player Player;' to check the value of Player[0].cash)

    cardName(Card); also doesnt reference and Player variables.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    > card hand[1];
    Arrays with one element and generally pretty pointless.
    Did you overflow it elsewhere.

    Also, make sure your deck has 52 cards in it, otherwise your loops will seriously trash someone elses memory.

  3. #3
    Registered User
    Join Date
    Feb 2006
    Posts
    4
    yeah i think i worked out most of my arrays were one too small. i thort arrays started at 0. i.e card[1] = 2 elements; card[0] and card[1]. It didnt generate any overflow errors but the output ofmy arrays were random.

  4. #4
    moi
    moi is offline
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    Quote Originally Posted by purebuu
    yeah i think i worked out most of my arrays were one too small. i thort arrays started at 0. i.e card[1] = 2 elements; card[0] and card[1]. It didnt generate any overflow errors but the output ofmy arrays were random.
    Array indicies start at 0, but the size of it is still 1 based; so char card[2] gives you spots card[0] and card[1]...
    hello, internet!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. auto detect variable value change - is it possible?
    By Yarin in forum C++ Programming
    Replies: 4
    Last Post: 07-17-2008, 12:41 PM
  2. c++builder6 change form names problem
    By Leite33 in forum C++ Programming
    Replies: 2
    Last Post: 06-09-2008, 08:20 AM
  3. controlling modifying global variable
    By sunny_master_07 in forum C Programming
    Replies: 9
    Last Post: 04-11-2008, 04:30 AM
  4. Replies: 2
    Last Post: 04-12-2004, 01:37 AM
  5. Replies: 2
    Last Post: 09-04-2001, 02:12 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21