as the title, I made a demo to describe my confusion.
if I replace [strcat ]to [stacpy], I can get what's obvious, which is
@
@
@
so I suspected the problem comes up with [strcat].
void main(){
for(int i=0; i<3 ;i++){
char* str = (char*)malloc(200*sizeof(char));
if(str == NULL){
printf("malloc failed. \n");
return;
}
strcat(str, "@ ");
printf("%s\n", str);
free(str);
str = NULL;
}
}
In my expectation, I should get:
@
@
@
but what I got is:
(1) @
xK`▒
xK`▒
and every time are not the same:
(2) @
x▒
x▒
(3) @
xk▒▒
xk▒▒