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* src_ptr = src;
    int* dest_ptr = dest;
    int count = n;

    while(count--){
        *dest_ptr++ = *src_ptr++;
    }
}

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

/*
Questions:
1. overlapping source & destination buffers?
2. Required to return anything from the function?
3. How to handle invalid inputs?
4. Is destination buffer large enough?


Plan:
1. Pointer walk (src_ptr and dest_ptr)
2. Loop over the array
3. Check for null pointer and n <= 0
4. Main has the tests

*/

 

 

Loading...

Input

5 10 20 30 40 50

Expected Output

10 20 30 40 50