. , , . , :
#include <stdlib.h>
#include <stdio.h>
int **load_matrix(int *colsref, int *rowsref)
{
int cols, rows, r, c;
scanf("%d%d", &rows, &cols);
int **res = malloc(sizeof (int *) * rows);
if (!res) return NULL;
for(r = 0; r < rows; r++) {
res[r] = malloc(sizeof (int) * cols);
if (!res[r]) {
int i, j;
for (i = 0; i < r; i++) free(res[i]);
free(res);
return NULL;
}
for (c = 0; c < cols; c++)
scanf("%d", &res[r][c]);
}
*colsref = cols; *rowsref = rows;
return res;
}
mat[row][col]. . , : ( )
#include <stdlib.h>
#include <stdio.h>
int **load_matrix(int *rowsref, int *colsref)
{
int cols, rows, r, c;
scanf("%d%d", &rows, &cols);
int **res = malloc(sizeof (int **) * rows);
if (!res) return NULL;
for(r = 0; r < rows; r++) {
res[r] = malloc(sizeof (int *) * cols);
if (!res[r]) {
int i, j;
for (i = 0; i < r; i++) free(res[i]);
free(res);
return NULL;
}
for (c = 0; c < cols; c++)
scanf("%d", &res[r][c]);
}
*colsref = cols; *rowsref = rows;
return res;
}
void print_matrix(int **matrix, int rows, int cols)
{
int r, c;
printf("%d %d\n", rows, cols);
for (r = 0; r < rows; r++) {
for (c = 0; c < cols; c++)
printf("\t%d", matrix[r][c]);
printf("\n");
}
}
void free_matrix(int **matrix, int rows)
{
int r;
for (r = 0; r < rows; r++)
free(matrix[r]);
free(matrix);
}
int main()
{
int **matrix, rows, cols;
matrix = load_matrix(&rows, &cols);
print_matrix(matrix, rows, cols);
free_matrix(matrix, rows);
}