[解决办法] 如果没记错的话,strncpy函数是要求 src和dest所指内存区域不可以重叠。如果重叠的话,那么结果是未定义的。也就是说,可以是对的,也可能是错的。 如果要处理有重叠的情况,可以用memmove [解决办法] The behavior of strncpy is undefined if the source and destination strings overlap.
看了下msdn的解释,确实是4#说的一样 [解决办法] 仅供参考
#include <stdio.h> #include <string.h> char s[256]; char *p; int r,n,i; int main() { while (1) { printf("请输入一行文字(空行结束),"%%20"将替换为" ","你懂得"将替换为"XXXXXX":\n"); fgets(s,256,stdin); if ('\n'==s[0]) break; p=s; while (1) { p=strstr(p,"%20"); if (p) { memmove(p+1,p+3,strlen(p)-3+1); p[0]=' '; } else break; } p=s; while (1) { p=strstr(p,"你懂得"); if (p) { memmove(p+6,p+6,strlen(p)-6+1);
string族的函数有很多神奇的特性,令我等防不胜防-;- [解决办法] There are two groups of string functions defined in the header <string.h>. The first have names beginning with str; the second have names beginning with mem. Except for memmove, the behavior is undefined if copying takes place between overlapping objects. Comparison functions treat arguments as unsigned char arrays.