Pass array to function using pointer Dry Run in C

Pass array to function 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.

Pass array to function using pointer Program Code

#include <stdio.h>
// Method 1: Using pointer parameter
void printArray(int *ptr, int size) {
    for(i = 0; i < size; i++) {
        printf("%d ", *(ptr + i));
    }
    printf("\n");
}

// Method 2: Using array parameter (actually same as pointer)
void modifyArray(int arr[], int size) {
    for(i = 0; i < size; i++) {
        arr[i] *= 2;  // Multiply each element by 2
    }
}

// Method 3: Using pointer with size
int getSum(int *ptr, int n) {
    int i;
    int sum = 0;
    for(i = 0; i < n; i++) {
        sum += ptr[i];  // Can use array notation too
    }
    return sum;
}

int main() {
    int arr[] = {10, 20, 30, 40, 50};
    int n = sizeof(arr)/sizeof(arr[0]);
    
    printf("Original array: ");
    printArray(arr, n);
    
    modifyArray(arr, n);
    printf("After doubling: ");
    printArray(arr, n);
    
    int sum = getSum(arr, n);
    printf("Sum of array: %d\n", sum);
    
    return 0;
}

View the complete Pointersarrays programs page.

Program Console Pass array to function using pointer Topic: C Vignaankosh.com
Execution Panel
Step 0/0
Console is empty.