Read the previous uknown matrix in C

The input of this function should be a pair of integers - sizematrices, followed by integers col*row. It looks something like this.

2 3
76 98 -31
30 30 32

This is the code that I have written so far. It works for row and column sizes, but when I try to pass a pointer to a readable matrix, it will work. I am confused about how I should pass an argument int** matrixto a function. (I saw an example of a similar function in a lecture, so I would prefer a solution using a double pointer.

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

int load_matrix(int *ro,int *co, int **matrix);

int main(int argc, char* argv[]) {
    int row = 3;
    int col = 3;
    int *ro = &row;
    int *co = &col;
    //int matx[1];
    int **matrix; //= matx[0];
    load_matrix(ro,co,matrix);
}

int load_matrix(int* ro,int* co, int** matrix) {
    int rows, cols;
    if(scanf("%d %d",&rows,&cols)==2) {
        int *mat = malloc(sizeof(int)*rows*cols);
        int read=0;
        for(int i = 0; i<rows &&read>=0; i++) {
            for(int j = 0; j<cols && read >=0; j++) {
                if(scanf("%d", &(mat[i*cols+j]))!=1) {
                    read = -1;
                } else {
                    read++;
                }
            }
        }
        if(read == (rows*cols)) {
            *ro = rows;
            *co = cols;
            **matrix = mat; --- this crashes the program
        } else {
            free(mat);
            *matrix = 0;
        }
    } else {
        return 100;
    }
    return 0;
}

As noted in the code, the part where it crashes is when I try to assign the pointer a int** matrixnew value to the address to which the read matrix is ​​allocated. How should I work with pointers here?

+4
3

2 :

  • matrix main int *matrix;, , matrix load_matrix.

  • load_matrix, mat *matrix = mat; **matrix = mat;

:

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

int load_matrix(int *ro, int *co, int **matrix) {
    int rows, cols;
    if (scanf("%d %d", &rows, &cols) == 2) {
        int *mat = malloc(sizeof(int) * rows * cols);
        int read = 0;
        for (int i = 0; i < rows && read >= 0; i++) {
            for (int j = 0; j < cols && read >= 0; j++) {
                if (scanf("%d", &mat[i * cols + j]) != 1) {
                    read = -1;
                } else {
                    read++;
                }
            }
        }
        if (read == rows * cols) {
            *ro = rows;
            *co = cols;
            *matrix = mat;
        } else {
            free(mat);
            *matrix = NULL;
        }
        return 0;
    } else {
        return 100;
    }
}

int main(int argc, char *argv[]) {
    int row, col, *matrix;
    load_matrix(&row, &col, &matrix);
    return 0;
}
+1

, matrix main. , :

int main(int argc, char* argv[]){
    int row;
    int col;
    int *matrix;
    if (load_matrix(&row, &col, &matrix) != 0) {
        // Report an error
        return -1;
    }
    printf("Got a matrix %d by %d\n", row, col);
    // ... 
    // At the end you should free the matrix
    free(matrix);
    return 0;
}

, &row &col , . matrix, , 2D-, .

+1

. , , . , :

#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); // array of pointers to rows.
    if (!res) return NULL; // no memory.
    for(r = 0; r < rows; r++) {
        res[r] = malloc(sizeof (int) * cols); // each row.
        if (!res[r]) {
            int i, j; // free all allocated memory
            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); // array of pointers to arrays.
    if (!res) return NULL;
    for(r = 0; r < rows; r++) {
            res[r] = malloc(sizeof (int *) * cols);
            if (!res[r]) {
                    int i, j; // free all allocated memory
                    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);
}
+1

Source: https://habr.com/ru/post/1695603/


All Articles