Jess Ahrens
Jess Ahrens

Reputation: 55

Indexing a pointer to char pointers in a Struct is overwriting all values?

I'm having a strange problem in C with a struct I'm working. One of the structs elements is a array of char arrays of size 3. The element is meant to be a players hand and the char arrays are meant to be the individual cards. They're formatted like "2C" where 2 is the rank and C is a club. My problems lies in a function that's creating the player hand from a given string. In the loop j is meant to be the card index and at the end it assigns the created card (which I know is correct) to the index. At the last iteration of the loop it assigns 4C and every one of the elements become 4C for some reason. Here is the code for the function:

typedef struct dataStruct {
int playerCount;
int handSize;
char playerID;
char** playerHand;
} dataStruct;

void proc_new_round(struct dataStruct* data, char* input) {
int length = strlen(input), i = 9, j = 0;
char tempChar[3];
if (length != 86 && length != 59 && length != 47)
    invalid_hub_message();
else if (input[8] != ' ')
    invalid_hub_message();
alloc_hand_size(data, (length - 8) / 3);
for (j = 0; j < data->handSize; j++) {
    if (input[i] != '2' && input[i] != '3' && input[i] != '4' &&
        input[i] != '5' && input[i] != '6' && input[i] != '7' &&
        input[i] != '8' && input[i] != '9' && input[i] != 'T' &&
        input[i] != 'J' && input[i] != 'Q' && input[i] != 'K' &&
        input[i] != 'A' ) {
        invalid_hub_message();
    }
    tempChar[0] = input[i++];
    if (input[i] != 'H' && input[i] != 'D' && input[i] != 'C' &&
        input[i] != 'S' ) {
        invalid_hub_message();
    }
    tempChar[1] = input[i++];
    tempChar[2] = '\0';
    printf("tempchar %s\n", tempChar);
    data->playerHand[j] = tempChar;
    if (i < length) {
        if (input[i++] != ',')
            invalid_hub_message();
    }
    printf("%d\n", j);
}
data->playerHand[5] = "7C";
printf("%s\n", data->playerHand[5]);
printf("%s\n", data->playerHand[12]);
printf("%s\n", data->playerHand[7]);
//print_hand(data);
}

The input given for the function is: newround 2C,2C,2C,2C,2C,2C,2C,2C,2C,2C,2C,2C,4C At the end of the function the 3 cards printed are 7C, 4C and 4C, but given the temp cards created it should be 7C, 4C and 2C. My print hand function also prints out every card except index 5 as 4C. Can someone tell me what's going on here?

Upvotes: 0

Views: 161

Answers (1)

melpomene
melpomene

Reputation: 85767

    data->playerHand[j] = tempChar;

All the pointers in data->playerHand have the same value and point into the same array, tempChar. Whatever is written last to tempChar appears as the final value of all.

Your code is analogous to this:

int *a[4];
int tmp;
int j;

for (j = 0; j < 4; j++) {
    tmp = j * 10;
    a[j] = &tmp;
}

int dummy = 42;
a[1] = &dummy;

printf("%d %d %d %d\n", *a[0], *a[1], *a[2], *a[3]);

All array elements are set to point to tmp by the loop. a[1] is then overwritten to point to dummy. The final values of tmp and dummy are 20 and 42, respectively, so the output is

20 42 20 20

Upvotes: 2

Related Questions