41. Simulate memcpy function Using Pointer Walk

#include <stdio.h>

void simulate_memcpy(int *dest, int *src, int n) {
    while (n--) {
        *dest++ = *src++;  // Copy and increment both pointers
    }
}

int main() {
    int n;
    scanf("%d", &n);

    int src[100], dest[100];
    for (int i = 0; i < n; i++) {
        scanf("%d", &src[i]);
    }

    simulate_memcpy(dest, src, n);

    for (int i = 0; i < n; i++) {
        printf("%d", dest[i]);
        if(i < n-1){
           printf(" "); 
        }
    }

    return 0;
}
  • The loop walks through memory directly
  • *dest++ = *src++ copies the value, then moves both pointers forward
  • This mimics how low-level memory copying works in actual memcpy()

 

Loading...

Input

5 10 20 30 40 50

Expected Output

10 20 30 40 50