Copy one array to another using pointer Dry Run in C

Copy one array to another using pointer is an interactive C dry run visualizer from the Pointersarrays programs section. Study the source code, then use the execution controls to follow each step, variable update, highlighted line, and console output.

This page provides a browser-based dry run with source-code highlighting, auto-scroll, voice narration controls, and execution output for learning the program step by step.

Copy one array to another using pointer Program Code

#include <stdio.h>
void copyArray(int *source, int *destination, int n) {
    for(i = 0; i < n; i++) {
        *(destination + i) = *(source + i);
    }
}

void copyArrayPtr(int *src, int *dest, int n) {
    for(i = 0; i < n; i++) {
        *dest = *src;
        src++;
        dest++;
    }
}

void copyArrayWhile(int *src, int *dest, int n) {
    int *end = src + n;
    while(src < end) {
        *dest++ = *src++;
    }
}

int main() {
    int i;
    int source[] = {10, 20, 30, 40, 50, 60, 70};
    int dest1[7], dest2[7], dest3[7];
    int n = sizeof(source)/sizeof(source[0]);
    
    copyArray(source, dest1, n);
    copyArrayPtr(source, dest2, n);
    copyArrayWhile(source, dest3, n);
    
    printf("Source: ");
    for(i = 0; i < n; i++) {
        printf("%d ", source[i]);
    }
    
    printf("\nDest1 (index): ");
    for(i = 0; i < n; i++) {
        printf("%d ", dest1[i]);
    }
    
    printf("\nDest2 (ptr move): ");
    for(i = 0; i < n; i++) {
        printf("%d ", dest2[i]);
    }
    
    printf("\nDest3 (while): ");
    for(i = 0; i < n; i++) {
        printf("%d ", dest3[i]);
    }
    
    return 0;
}

View the complete Pointersarrays programs page.

Program Console Copy one array to another using pointer Topic: C Vignaankosh.com
Execution Panel
Step 0/0
Console is empty.