简体   繁体   中英

How to properly pass dynamic 2-d array into a function in C

I need to dynamically allocate a 2-d array in a function with n rows and m columns that are also has to be read from input function.

int main()
{
    int** matrix;
    int n, m;
    input(matrix, &n, &m);
    return 0;
} 

void input(int** matrix, int* n, int* m) {
    if (scanf("%d", n) == 1 && getchar() == '\n') {
        if (*n <= 0) {
            error = 1;
        }
    }
    if (error == 0) {
        if (scanf("%d", m) == 1 && getchar() == '\n') {
            if (*m <= 0) {
                error = 1;
            }
        }
    }
    if (error == 0) {
        matrix = (int**)calloc(*n, sizeof(int*));
        for (int i = 0; i < *n; i++) {
            matrix[i] = (int*)calloc(*m, sizeof(int));
        }
        for (int i = 0; i < *n && error == 0; i++) {
            for (int j = 0; j < *m && error == 0; j++) {
                if (scanf("%d", &matrix[i][j]) == 1) {
                }
                else {
                    error = 1;
                } 
            }
        }
        for (int i = 0; i < *n; i++) {
            for (int j = 0; j < *m; j++) {
                printf("%d ", matrix[i][j]);
            }
            printf("\n");
        }
    }
}

It scans and prints values properly, but if I try to interact with matrix outside this function or in another function (eg output(int** matrix, int *n, int *m that just prints all the values) I'll get memory error. How do i suppose to fix that? (I need to keep double pointer).

The value of the matrix is not being used by the input function, and main is not passing any valid value for this parameter.

Since the return type of input is void , everything needs to be passed back to the caller via pointer arguments. The values for n and m are already being passed back by pointers, so the value of matrix also needs to be passed back by a pointer, like this:

    int* matrix;
    int n, m;
    input(&matrix, &n, &m);

Note that the type of the caller's matrix variable needed to be changed from int** to int* . This means that the matrix must be represented by a linear (1-D) array of int . Therefore, input needs to allocate the memory for the matrix as a single block holding an array of int and it needs to map the 2-D matrix element indices to a 1-D array index.

The allocation in input needs to be something like this:

        *matrix = calloc(*n * *m, sizeof(int));

and the matrix element at row i column j needs to be converted to a 1-D index like this: (*matrix)[i * *m + j] (where *m is the number of columns).

Other functions that are called on the same matrix need to do the same conversion from 2-D indices to a 1-D index. For example:

void output(const int *matrix, int n, int m) {
   for (int i = 0; i < n; i++) {
       for (int j = 0; j < m; j++) {
           printf("%d ", matrix[i * m + j]);
       }
       printf("\n");
   }
}

Example usage:

    int* matrix;
    int n, m;
    input(&matrix, &n, &m);
    output(matrix, n, m);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM