Sum of array using pointer Dry Run in C

Sum of array using pointer is an interactive C dry run visualizer from the Pointersintro 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.

Sum of array using pointer Program Code

#include <stdio.h>
int main() {
    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(int i = 0; i < n; i++) {
        sum += *(ptr + i);
    }
    printf("Sum using *(ptr + i): %d\n", sum);
    
    // Method 2: Using pointer increment
    sum = 0;
    ptr = arr;
    for(int i = 0; i < n; i++) {
        sum += *ptr;
        ptr++;
    }
    printf("Sum using ptr++: %d\n", sum);
    
    // Method 3: Using while loop with end pointer
    sum = 0;
    ptr = arr;
    int *end = arr + n;
    while(ptr < end) {
        sum += *ptr++;
    }
    printf("Sum using while: %d\n", sum);
    
    return 0;
}

View the complete Pointersintro programs page.

Program Console Sum of array using pointer Topic: C Vignaankosh.com
Execution Panel
Step 0/0
Console is empty.