Find sum of array using pointers Dry Run in C

Find sum of array using pointers 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.

Find sum of array using pointers Program Code

#include <stdio.h>
int main() {
    int i;
    int arr[] = {10, 20, 30, 40, 50, 60, 70};
    int n = sizeof(arr)/sizeof(arr[0]);
    int sum = 0;
    int *ptr = arr;
    
    // Method 1: Using pointer arithmetic
    for(i = 0; i < n; i++) {
        sum += *(ptr + i);
    }
    printf("Sum (method 1): %d\n", sum);
    
    // Method 2: Moving pointer
    sum = 0;
    ptr = arr;
    for(i = 0; i < n; i++) {
        sum += *ptr;
        ptr++;
    }
    printf("Sum (method 2): %d\n", sum);
    
    // Method 3: While loop with end pointer
    sum = 0;
    ptr = arr;
    int *end = arr + n;
    while(ptr < end) {
        sum += *ptr++;
    }
    printf("Sum (method 3): %d\n", sum);
    
    return 0;
}

View the complete Pointersarrays programs page.

Program Console Find sum of array using pointers Topic: C Vignaankosh.com
Execution Panel
Step 0/0
Console is empty.