All submissions

Simulate memcpy function Using Pointer Walk

Code

#include <stdio.h>
void simulate_memcpy(int *dest, int *src, int n) {
    // Your logic here
   // int *d = dest;
    //int *s = src;
    while(n--)
    {
       // *d++ = *s++;
       *dest++ = *src++;
    }
}

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;
}

Solving Approach

  • Initialize two pointers: one to source, one to destination.(can skip this)
  • Use a loop: while (n--) *dest++ = *src++; — copy and advance both pointers.

 

 

Loading...

Input

5 10 20 30 40 50

Expected Output

10 20 30 40 50