#include <stdio.h>
#include <stdlib.h>
int main() {
int i, j;
int a = 3, b = 3; // 行数a、列数bを固定(例として3x3の配列)
int **mat;
// 2次元配列の動的確保
mat
= (int**)malloc(sizeof(int*) * a
); // 行のためのメモリ確保 if (mat == NULL) {
printf("Memory allocation failed!\n"); return 1;
}
// 各行に対して列のためのメモリ確保
for (i = 0; i < a; i++) {
mat
[i
] = (int*)malloc(sizeof(int) * b
); // 列のメモリ確保 if (mat[i] == NULL) {
printf("Memory allocation failed!\n"); return 1;
}
}
// 1から9までの数値を2次元配列に代入
int num = 1; // 数値は1からスタート
for (i = 0; i < a; i++) {
for (j = 0; j < b; j++) {
mat[i][j] = num++; // numを代入してからインクリメント
}
}
// 配列の内容を表示
for (i = 0; i < a; i++) {
for (j = 0; j < b; j++) {
}
}
// メモリの解放
for (i = 0; i < a; i++) {
free(mat
[i
]); // 各行に対して解放 }
free(mat
); // 行のポインタ自体を解放
return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KCmludCBtYWluKCkgewogICAgaW50IGksIGo7CiAgICBpbnQgYSA9IDMsIGIgPSAzOyAgLy8g6KGM5pWwYeOAgeWIl+aVsGLjgpLlm7rlrprvvIjkvovjgajjgZfjgaYzeDPjga7phY3liJfvvIkKICAgIGludCAqKm1hdDsKCiAgICAvLyAy5qyh5YWD6YWN5YiX44Gu5YuV55qE56K65L+dCiAgICBtYXQgPSAoaW50KiopbWFsbG9jKHNpemVvZihpbnQqKSAqIGEpOyAgLy8g6KGM44Gu44Gf44KB44Gu44Oh44Oi44Oq56K65L+dCiAgICBpZiAobWF0ID09IE5VTEwpIHsKICAgICAgICBwcmludGYoIk1lbW9yeSBhbGxvY2F0aW9uIGZhaWxlZCFcbiIpOwogICAgICAgIHJldHVybiAxOwogICAgfQoKICAgIC8vIOWQhOihjOOBq+WvvuOBl+OBpuWIl+OBruOBn+OCgeOBruODoeODouODqueiuuS/nQogICAgZm9yIChpID0gMDsgaSA8IGE7IGkrKykgewogICAgICAgIG1hdFtpXSA9IChpbnQqKW1hbGxvYyhzaXplb2YoaW50KSAqIGIpOyAgLy8g5YiX44Gu44Oh44Oi44Oq56K65L+dCiAgICAgICAgaWYgKG1hdFtpXSA9PSBOVUxMKSB7CiAgICAgICAgICAgIHByaW50ZigiTWVtb3J5IGFsbG9jYXRpb24gZmFpbGVkIVxuIik7CiAgICAgICAgICAgIHJldHVybiAxOwogICAgICAgIH0KICAgIH0KCiAgICAvLyAx44GL44KJOeOBvuOBp+OBruaVsOWApOOCkjLmrKHlhYPphY3liJfjgavku6PlhaUKICAgIGludCBudW0gPSAxOyAgLy8g5pWw5YCk44GvMeOBi+OCieOCueOCv+ODvOODiAogICAgZm9yIChpID0gMDsgaSA8IGE7IGkrKykgewogICAgICAgIGZvciAoaiA9IDA7IGogPCBiOyBqKyspIHsKICAgICAgICAgICAgbWF0W2ldW2pdID0gbnVtKys7ICAvLyBudW3jgpLku6PlhaXjgZfjgabjgYvjgonjgqTjg7Pjgq/jg6rjg6Hjg7Pjg4gKICAgICAgICB9CiAgICB9CgogICAgLy8g6YWN5YiX44Gu5YaF5a6544KS6KGo56S6CiAgICBmb3IgKGkgPSAwOyBpIDwgYTsgaSsrKSB7CiAgICAgICAgZm9yIChqID0gMDsgaiA8IGI7IGorKykgewogICAgICAgICAgICBwcmludGYoIiVkICIsIG1hdFtpXVtqXSk7CiAgICAgICAgfQogICAgICAgIHByaW50ZigiXG4iKTsKICAgIH0KCiAgICAvLyDjg6Hjg6Ljg6rjga7op6PmlL4KICAgIGZvciAoaSA9IDA7IGkgPCBhOyBpKyspIHsKICAgICAgICBmcmVlKG1hdFtpXSk7ICAvLyDlkITooYzjgavlr77jgZfjgabop6PmlL4KICAgIH0KICAgIGZyZWUobWF0KTsgIC8vIOihjOOBruODneOCpOODs+OCv+iHquS9k+OCkuino+aUvgoKICAgIHJldHVybiAwOwp9Cg==