I have a question about using strcpy. I know the ANSI C standard says : source and destination must not overlap, otherwise the behaviour is unpredictable. I show you a piece of code that works as I expect if it is compiled using an old gnu C compiler under Linux.
#include <string.h>
#include <stdio.h>
char S[80],*P;
int main() {
strcpy(S,"abcdefghi\r\njklmnopqr\r\nstuvwxyz\r\n");
for (P=S; P=strchr(P,'\r'); P++) strcpy(P,P+1);
printf("%s\n",S);
return 0;
}
This sequence removes every \r
(carriage return) from the input string. I know (from Kernigham and Ritchie) that a very simple implementation for strcpy is the following
while (*t++=*s++) ;
Now I compiled my program using gcc (Gentoo 4.5.4 p1.0, pie-0.4.7) 4.5.4 and it prints this:
abcdefghi
jklmnpqr <-- missing 'o'
stuvwxxyz <-- doubled 'x'
I suppose this compiler (in fact its library) uses a very sophisticated sequence for strcpy
, and I don't understand the reason.
long long *
) and copy that. This means that the copy overwrite what is being copied. – Loginov