1

Need help understanding what I am doing wrong. This is part of an assignment that I am doing through distance learning. Thanks.

I am passing a pointer and an array of players (2 players). Where players are a struct:

 struct player
{
        char name[NAMELEN + 1];
        int score; /* count of number of pieces on the board */
        colour token_colour;
};

struct player * play_game(struct player players[])
{
        gameboard board;
        struct player * current, *other;

        gameboard_init(board);
        player_init(&players[0],1);
        player_init(&players[1],2);

        **randomize_game(players,current);**

        /* replace this NULL return value with the appropriate return value */
        return NULL;
}

I then attempt to set the pointer(curplayer) to the player that won the randomize roll to go first. I am stepping through using GDB and can confirm that the value is the same before as it is after the command. Please help! with explanations of what im doing wrong:

curplayer= &players[0];

void randomize_game(struct player players[],struct player* curplayer)
{
        int p;
        time_t t;

        srand((unsigned) time(&t));
        p = rand() % 2;
        if(p==0){
                **curplayer= &players[0];**
                players[0].token_colour = CC_WHITE;
                players[1].token_colour = CC_RED;
        }else{
                **curplayer= &players[1];**
                players[0].token_colour = CC_RED;
                players[1].token_colour = CC_WHITE;
        }

}

Thanks

ClintB
  • 509
  • 3
  • 6
  • 1
    Classic pitfall: `randomize_game(players,current);` cannot modify `current` at all. Parameters are passed by value in C. – Jabberwocky Jan 05 '17 at 09:50
  • Same problem as explained [here](http://stackoverflow.com/questions/39486797/dynamic-memory-access-only-works-inside-function). – Lundin Jan 05 '17 at 09:51

1 Answers1

0

You want to modify current, but curplayer is a pointer that is local to randomize_game, it is only initialized with the value in current. Any change you make to it is not applied to current in play_game.

If you want to modify any variable from a calling context, you need to pass its address. This applies to pointers as well.

void randomize_game(struct player players[],struct player **curplayer)
{
        int p;
        time_t t;

        srand((unsigned) time(&t));
        p = rand() % 2;
        if(p==0){
                *curplayer= &players[0];
                players[0].token_colour = CC_WHITE;
                players[1].token_colour = CC_RED;
        }else{
                *curplayer= &players[1];
                players[0].token_colour = CC_RED;
                players[1].token_colour = CC_WHITE;
        }
}

Of course, you need to change how you pass the variable:

randomize_game(players, &current);
StoryTeller - Unslander Monica
  • 165,132
  • 21
  • 377
  • 458
  • I have made those changes but the value still doesn't update. Here is the GCD debugging. Immediatly after the if statment in randomize_game: (gdb) print curplayer $1 = (struct player **) 0x0 (gdb) print *curplayer Cannot access memory at address 0x0 – ClintB Jan 05 '17 at 10:05
  • @ClintB - See my edit. And heed your compiler warnings from now on. – StoryTeller - Unslander Monica Jan 05 '17 at 10:08
  • Legend! I got it working. Been stuck on this for hours. Thanks. FYI I have a lot of warnings coming up atm because so many unused variables, so was ignoring the warnings for the moment. Lesson well and truly learnt. – ClintB Jan 05 '17 at 10:10
  • @ClintB - The greatest favor you can do for yourself is to bump warnings to an error status (and turn on as many as possible). It will save you a lot of headaches in the long run, even if initially there is a lot to fix. – StoryTeller - Unslander Monica Jan 05 '17 at 10:15