Currently, I need to strcat() 2 strings together. The catch is that I have to do this 3 times. (Total of 6 concatenations). The procedure is this, repeated 3 times using loops:
- Malloc a string
- Using for loop, call strcat 2 times
- Free the string
The problem is that even after I free the string and re-malloc, the strcat seems to keep on concatenating the previous string.
For example:
Expected Output from AA BB CC DD EE FF
- strcat string 1: AABB
- strcat string 2: CCDD
- strcat string 3: EEFF
Actual Output:
- strcat string 1: AABB
- strcat string 2: AABBCCDD
- strcat string 3: AABBCCDDEEFF
Does anyone know why it's doing this?
void sendInitialHand(card * deck) {
char * stringToSend;
playerNode * curNode;
curNode = housePlayers.head;
for (int i=0; i<housePlayers.playerCount; i++) {
stringToSend = malloc(sizeof(char)*6);
for (int j=0; j<52; j++) {
if (deck[j].inPlay == curNode->playerFD) {
strcat(stringToSend, deck[j].identifier);
}
}
for (int j=0; j<52; j++) {
if (deck[j].inPlay == 10) {
strcat(stringToSend, deck[j].identifier);
}
}
printf("[NETWORK] Send %d the following: %s\n", curNode->playerFD, stringToSend);
//send(curNode->playerFD, stringToSend, 6, 0);
free(stringToSend);
curNode = curNode->next;
}
}