[英]How can i copy the contents of one variable to another using pointers?
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 []具有相同的字符
我該如何使用指針呢?
只是
while (*src != '\0')
*dst++ = *src++;
*dst = '\0'; // Perhaps you will also need this!
請記住,在循環結束時, dst
指向數組的末尾而不是開頭。 還要復制'\\0'
終止符,還是不想?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.