char *foo(char *dest, const char *src)
{
unsigned i;
for (i=0; src[i] != '\0'; ++i)
dest[i] = src[i];
dest[i] = '\0';
return dest;
}
我知道代码正在通过两个变量的数组移动,而它正在这样做,它使dest []与src []
具有相同的字符我怎么能用指针代替呢?
答案 0 :(得分:3)
简单地
while (*src != '\0')
*dst++ = *src++;
*dst = '\0'; // Perhaps you will also need this!
请记住,在循环结束时dst
指向数组的末尾而不是开头。还要复制'\0'
终止符,或者你不想复制?