1. 程式人生 > >編寫函式,模擬實現memmove

編寫函式,模擬實現memmove


編寫函式,模擬實現memmove


程式程式碼如下:



#include <stdio.h>
#include <assert.h>

void *MemMove(void *dest, const void *src, size_t count)
{
    assert(dest);
    assert(src);

    void *ret = dest;

    if (dest <= src)
    {
        while (count--)
        {
            *(char *)dest = *(char
*)src; dest = (char *)dest + 1; src = (char *)src + 1; } } else { dest = (char *)dest + count - 1; src = (char *)src + count - 1; while (count--) { *(char *)dest = *(char *)src; dest = (char *)dest - 1
; src = (char *)src - 1; } } return ret; } int main() { int arr[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; int i = 0; void * r = MemMove(arr + 2, arr, 4 * sizeof(int)); for (i = 0; i < sizeof(arr) / sizeof(arr[0]); i++) { printf("%d ", arr[i]); } printf
("\n"); return 0; }

程式執行結果如下:


這裡寫圖片描述