How to pass a matrix as a reference to a function?

Asked

Viewed 2,190 times

3

I’m trying to pass as a parameter a matrix for a function as a reference, and in this function do operations in this matrix but I’m not getting. I don’t want you to use define or const int for the matrix dimension values because I want these values to change.

    #include <stdio.h>


    int operacao (int *matriz[], int dim){
        int i=0;
        int j=0;
        for (i=0; i<dim; i++){
            for (j=0; j<dim; j++) {
                matriz[i][j] = 1;
                printf("%d,", matriz[i][j]);
            }
        }
    }

    int main () {
        int dimencao;
        printf ("digite a dimencao da matriz:");
        scanf("%d", &dimencao);
        int matriz[dimencao][dimencao];
        operacao(&matriz,dimencao);
        return 0;
    }

2 answers

3


One simple way to do this is to change the order of the parameters so that the dimension comes before the matrix, and with this you can already use the dimension in the parameter that represents the matrix.

Example:

int operacao (int dim, int matriz[dim][dim]) { 
//                |                 ^---^ matriz utiliza o dim do parametro anterior
//                ^ ---- a dimensão agora vem como primeiro parametro
    int i=0;
    int j=0;
    for (i=0; i<dim; i++) {
        for (j=0; j<dim; j++) {
            matriz[i][j] = 1;
            printf("%d,", matriz[i][j]);
        }
    }
    return 0;
}

int main () {
    int dimencao;
    printf ("digite a dimencao da matriz:");
    scanf("%d", &dimencao);
    int matriz[dimencao][dimencao];
    operacao(dimencao, matriz); //sem & na matriz, e agora com a ordem invertida
    return 0;
}

I only changed the lines that were commented.

Watch it work on Ideone

3

C is a language without many abstractions, so you need to control each aspect directly and do what you don’t want to do or create your own abstractions or mechanism to control it.

You can pass the size as argument of the function (see example in Isac’s answer) or you can create a structure where you have the size and the reference for the matrix, so it goes straight through this structure and it is in it that you will manipulate everything. In fact this is the most commonly used form in real codes. The previous form is also used in some simpler cases. I prefer the structure that’s very flexible, something like that:

typedef {
    size_t size;
    int **matrix;
} Matriz;

I put in the Github for future reference.

You will have to make the allocation with malloc(), but out of exercise is almost always what I would do. It is rare to create a array as a matrix in the stack and pass as parameter. If it is just exercise do it in the simplest way and use a define, if it is not create the way it can actually be used universally.

This shape you don’t want to use is only used in exercises. A lot of what you’ll find on the internet, in books and most sources is just exercise code, it’s not really how you program. But most people won’t program in real C.

  • Thanks for the answer, yours and Isac’s helped me solve the problem.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.