user3603183
user3603183

Reputation: 333

Pointing one struct to another? (not permanent)

I'm struggling to get one struct to point to another which is dependent on the arguments passed into the command line, the problem is, that struct me is appearing to be pointing correctly to the desired struct in the initialise game function, (which is called by parse args which is called in main), however, when I print their address after the function call, in main, it appears not to have changed (following output) if the player is A:

Before initialise: 0x7f8a88403990, 0x7f8a884039f0, 0x7f8a88403a50, 0x7f8a88403ab0, 0x7f8a88403b10
After initialise: 0x7f8a884039f0, 0x7f8a884039f0, 0x7f8a88403a50, 0x7f8a88403ab0, 0x7f8a88403b10
After parse args: 0x7f8a88403990, 0x7f8a884039f0, 0x7f8a88403a50, 0x7f8a88403ab0, 0x7f8a88403b10

int main (int argc, char *argv[]) {

Player *me = NULL, *playerA = NULL;
Player *playerB = NULL, *playerC = NULL, *playerD = NULL;

me = malloc(sizeof(*me));
playerA = malloc(sizeof(*playerA));
playerB = malloc(sizeof(*playerB));
playerC = malloc(sizeof(*playerC));
playerD = malloc(sizeof(*playerD));

parse_args(me, playerA, playerB, playerC, playerD, argv);

//should be pointing to the same memory location
printf("After parse args: %p, %p, %p, %p, %p\n", me, playerA, playerB, playerC, playerD);

}

void parse_args(Player *me, Player *a, Player *b, Player *c, Player *d,
            char *argv[]) {
initialise_game(*tempChar, tempNum, me, a, b, c, d);

}

void initialise_game(char playerID, int numPlayers, Player *me, Player *a,
                 Player *b, Player *c, Player *d) {
printf("Before initialise: %p, %p, %p, %p, %p\n", me, a, b, c, d);

switch((int)playerID) {
    case 'A':
        me = a;
        break;
    case 'B':
        me = b;
        break;
    case 'C':
        if (numPlayers < 3) {
            exit_prog(EXIT_PLAYERID);
        }
        me = c;
        break;
    case 'D':
        if (numPlayers < 4) {
            exit_prog(EXIT_PLAYERID);
        }
        me = d;
        break;
}

printf("After initialise: %p, %p, %p, %p, %p\n", me, a, b, c, d);

}

Upvotes: 0

Views: 62

Answers (2)

Michael Petch
Michael Petch

Reputation: 47573

Pointers get passed by value. That means you can't modify them in the body of a function(). See this StackOverflow question/answer

I think you wanted to do something like:

void parse_args(Player **me, Player *a, Player *b, Player *c, Player *d, char *argv[]) {
    initialise_game(*tempChar, tempNum, me, a, b, c, d);
}

void initialise_game(char playerID, int numPlayers, Player **me, Player *a,
             Player *b, Player *c, Player *d) 
{
    printf("Before initialise: %p, %p, %p, %p, %p\n", me, a, b, c, d);
    switch((int)playerID) {
    case 'A':
        *me = a;
        break;
    case 'B':
        *me = b;
    break;
    case 'C':
        if (numPlayers < 3) {
            exit_prog(EXIT_PLAYERID);
        }
        *me = c;
        break;
    case 'D':
        if (numPlayers < 4) {
            exit_prog(EXIT_PLAYERID);
        }
        *me = d;
        break;
}

Your call to parse_args would need to pass a pointer to a pointer for me:

parse_args(&me, playerA, playerB, playerC, playerD, argv);

Upvotes: 1

ouah
ouah

Reputation: 145829

You need to use a pointer to a pointer to structure for function parameters (e.g., Player **x). Using a pointer to the structure you can only modify the structure object, not the pointer object. Remember all function arguments are passed by value in C, if you modify the pointer argument in the function you are only modifying a copy of the pointer.

Upvotes: 0

Related Questions