#include <stdio.h>
#include <stdlib.h>

int main(void) {
    int rows = 3; // 行数
    int cols = 4; // 列数
    int i, j;

    // --- Step 1: ポインタの配列（行）を確保 ---
    int **a = (int **)malloc(sizeof(int *) * rows);
    if (a == NULL) return 1;

    // --- Step 2: 各行に実データ（列）の領域を確保 ---
    for (i = 0; i < rows; i++) {
        a[i] = (int *)malloc(sizeof(int) * cols);
        if (a[i] == NULL) return 1;
    }

    // 値の代入と表示の例
    for (i = 0; i < rows; i++) {
        for (j = 0; j < cols; j++) {
            a[i][j] = i * cols + j;
            printf("%2d ", a[i][j]);
        }
        printf("\n");
    }

    // メモリの解放（確保と逆の順番で解放する）
    for (i = 0; i < rows; i++) {
        free(a[i]); // Step 2で確保した分を解放
    }
    free(a);        // Step 1で確保した分を解放

    return 0;
}