Traverse matrix using pointer Dry Run in C

Traverse matrix 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.

Traverse matrix 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("Row-wise traversal:\n");
    int (*rowPtr)[4] = matrix;
    for(i = 0; i < rows; i++) {
        for(j = 0; j < cols; j++) {
            printf("%3d ", rowPtr[i][j]);
        }
        printf("\n");
    }
    
    printf("\nColumn-wise traversal:\n");
    for(j = 0; j < cols; j++) {
        for(i = 0; i < rows; i++) {
            printf("%3d ", *(*(matrix + i) + j));
        }
        printf("\n");
    }
    
    printf("\nLinear traversal (single pointer):\n");
    int *ptr = &matrix[0][0];
    for(i = 0; i < rows * cols; i++) {
        printf("%3d ", ptr[i]);
        if((i+1) % cols == 0) printf("\n");
    }
    
    return 0;
}

View the complete Pointersarrays programs page.

Program Console Traverse matrix using pointer Topic: C Vignaankosh.com
Execution Panel
Step 0/0
Console is empty.