Matrix transpose using pointer Dry Run in C

Matrix transpose 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.

Matrix transpose using pointer Program Code

#include <stdio.h>
void transpose(int (*source)[4], int (*dest)[3], int rows, int cols) {
    int j;
    for(i = 0; i < rows; i++) {
        for(j = 0; j < cols; j++) {
            dest[j][i] = source[i][j];
        }
    }
}

void transposePtr(int *src, int *dest, int rows, int cols) {
    int j;
    for(i = 0; i < rows; i++) {
        for(j = 0; j < cols; j++) {
            dest[j * rows + i] = src[i * cols + j];
        }
    }
}

int main() {
    int i, j;
    int matrix[3][4] = {
        {1, 2, 3, 4},
        {5, 6, 7, 8},
        {9, 10, 11, 12}
    };
    int transpose[4][3];
    int rows = 3, cols = 4;
    
    printf("Original matrix (3×4):\n");
    for(i = 0; i < rows; i++) {
        for(j = 0; j < cols; j++) {
            printf("%3d ", matrix[i][j]);
        }
        printf("\n");
    }
    
    // Method 1: Using 2D array pointers
    transpose(matrix, transpose, rows, cols);
    
    printf("\nTranspose (4×3) using row pointer:\n");
    for(i = 0; i < cols; i++) {
        for(j = 0; j < rows; j++) {
            printf("%3d ", transpose[i][j]);
        }
        printf("\n");
    }
    
    // Method 2: Using single pointer
    int transpose2[4][3];
    transposePtr(&matrix[0][0], &transpose2[0][0], rows, cols);
    
    printf("\nTranspose using single pointer:\n");
    for(i = 0; i < cols; i++) {
        for(j = 0; j < rows; j++) {
            printf("%3d ", transpose2[i][j]);
        }
        printf("\n");
    }
    
    return 0;
}

View the complete Pointersarrays programs page.

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