Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

C Structure/pointer troubleshooting

Options
  • 18-08-2006 10:48am
    #1
    Registered Users Posts: 1,328 ✭✭✭


    Hi, I just started writing what's currently a simple little card game in C. At the moment I just have a function to shuffle a deck of cards (ie. put numbers between 0 and 51 randomly in an array). And then to "deal" the deck.. by just assigning pointers for each players hand to point to different lengths along the array holding the deck.

    To make everything as portable and reusable as possible, I'm passing the addresses of the structures containing the information to the functions.

    I have a structure called gameinfo (game) which basically just stores some basic information about the game in progress (how many players, and an array of another structure called "hand" which will for each player in the game contain information about how many cards they have left, and what cards they are.

    For the deal function.. rather than run a loop through the cards and the players and copy elements from the deck[] array into new arrays allocated for each player.. I figured it would be neater to just have the "cards" pointer in each hand structure point towards a different part of the deck array evenly spaced along the array.

    The peculiar thing however is.. even tho the deal function does not actually manipulate data.. it just reassigns pointers to point to different parts of the deck[] array, after making a call to the function.. if I print out the contents of the deck[] array again.. I find that it has screwed up elements 0 and 2 of the array. This happens when the playernumber argument is set to 13, and not to 4.

    I figure this is down to some basic misunderstanding I have about how pointers to structures work or something like that.. but I cant work it out.. anybody have any ideas?

    Here's the code

    Btw the deal function isnt supposed to deal out the whole deck.. there maybe left over cards that didnt divide.
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #include <math.h>
    
    #define MAXPLAYERS 10;
    
    struct hand {
    
    	int number;
    	int* cards;
    	
    };
    
    struct gameinfo {
    
    	int playernumber;
    	struct hand hands[MAXPLAYERS];
    
    };
    
    void generateDeck(int*);
    void deal(int*,int,gameinfo*);
    
    int main()
    {
    	int i,j;
    	int deck[52];
    	struct gameinfo game;
    
    	generateDeck(deck);
    
    	for(i=0;i<52;i++) printf("Card number %i : %i\n",i,deck[i]);
    
    	deal(deck,13,&game); // Using 13 with this function does crazy stuff
    	//deal(deck,4,&game);   // Using 4 in this function is fine.. everything works great
    
    	for(i=0;i<52;i++) printf("Card number %i : %i\n",i,deck[i]); //print again so you see what happens
    	
    	return 0;
    }
    
    void generateDeck(int* deck) {
    
    	srand(time(NULL));
    	int taken[52];
    	int number,i;
    	
    	for(i=0;i<52;i++) taken[i] = 0;
    
    	for(i=0;i<52;i++) {
    		
    		do {
    			number = rand() % 52;
    		} while(taken[number]);
    		taken[number] = 1;
    		
    		deck[i] = number;	
    	}
    }
    
    void deal(int* deck, int players, gameinfo* game)
    {
    	int i;
    	int handsize = 52 / players;
    	game->playernumber = players;
    
    	for(i=0;i<players;i++) {
    	
    		game->hands[i].number = handsize;
    		game->hands[i].cards = deck + i*handsize;
    
    	}
    	
    }
    


Comments

  • Registered Users Posts: 6,508 ✭✭✭daymobrew


    You might have already figured out that this line is the problem:
    game->hands[i].cards = deck + i*handsize;
    
    My brain must be fried. When I moved the generateDesk and deal function code into main() it still had the same error - so it is not related to the passing of pointers. Jeez, I hate pointers.


  • Registered Users Posts: 1,328 ✭✭✭Sev


    No wait.. goddamn.. I'm an idiot.

    I made my MAXPLAYERS 10. So it only allocates memory for 10 "hands" structures. So if I pass 13 as the number of players to the deal function.. it at some point tries to write to game->hands[10].cards.. so of course thats going to generate problems.

    Thanks for the help.


  • Registered Users Posts: 6,508 ✭✭✭daymobrew


    I find it strange that the ***ev debugger didn't report an access violation.

    Edit: I mentioned the MicroSoft DEVeloper studio compiler and it blanked out the M, the S and the D. Strange.


  • Registered Users Posts: 5,335 ✭✭✭Cake Fiend


    Did you possibly type a 'C' there instead of an 'S'?
    That particular three-letter combination is banned because *** are a shower of ****s.


  • Registered Users Posts: 6,508 ✭✭✭daymobrew


    Sev wrote:
    I made my MAXPLAYERS 10. So it only allocates memory for 10 "hands" structures.
    You should add a check to the deal() function to check this.


  • Advertisement
Advertisement