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);
}