下面的代码片段显示了memmove()
.
void my_memmove(void* dest, const void* src, size_t size)
{
unsigned int i;
char* d = (char*)dest;
char* s = (char*)src;
if( s > d )
{
for( i = 0; s[i] && i < size; ++i )
d[i] = s[i];
}
else
for( i = size-1; d[i] && i >= 0; --i )
d[i] = s[i];
}
int main()
{
char my_str[] = "abcdefgh";
char str[] = "abcdefgh";
my_memmove(my_str+1, my_str, 4);
memmove(str+1, str, 4);
printf("%s %s\n", my_str, str);
return 0;
}
我得到的输出为:
aabcdfgh
为什么 my_memmove() 无法正常工作(它更改了 my_str 使其输出空字符串)?