Access 2D array using pointer Dry Run in C

Access 2D array 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.

Access 2D array using pointer Program Code

#include <stdio.h>
int main() {
    int i, j;
    int matrix[3][4] = {
        {1, 2, 3, 4},
        {5, 6, 7, 8},
        {9, 10, 11, 12}
    };
    
    int rows = 3, cols = 4;
    
    printf("Accessing 2D array using pointers:\n\n");
    
    // Method 1: Using row pointer
    int (*rowPtr)[4] = matrix;  // Pointer to array of 4 ints
    
    for(i = 0; i < rows; i++) {
        for(j = 0; j < cols; j++) {
            printf("%2d ", rowPtr[i][j]);
        }
        printf("\n");
    }
    
    // Method 2: Using single pointer with offset calculation
    printf("\nUsing single pointer (row-major order):\n");
    int *ptr = &matrix[0][0];
    for(i = 0; i < rows * cols; i++) {
        printf("%2d ", *(ptr + i));
        if((i+1) % cols == 0) printf("\n");
    }
    
    // Method 3: Pointer to element using arithmetic
    printf("\nUsing *(*(matrix + i) + j):\n");
    for(i = 0; i < rows; i++) {
        for(j = 0; j < cols; j++) {
            printf("%2d ", *(*(matrix + i) + j));
        }
        printf("\n");
    }
    
    return 0;
}

View the complete Pointersarrays programs page.

Program Console Access 2D array using pointer Topic: C Vignaankosh.com
Execution Panel
Step 0/0
Console is empty.