How to pass an array as a reference to a function?

3

I'm trying to pass an array as a parameter to a function as a reference, and in this function I do operations on that array but I'm not getting it. I do not want you to use define or const int for the array 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;
    }
    
asked by anonymous 30.09.2018 / 16:16

2 answers

3

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

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've just changed the lines that have been commented out.

See it working on Ideone

    
30.09.2018 / 16:37
3

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

You can pass the size as an argument to the function (see example in Isac's response) or you can create a structure where you have the size and reference for the array, so it passes directly through this structure and it is going to manipulate everything. In fact this is the most used form in real codes. The previous form is also used in some simpler cases. I prefer the structure which is very flexible, something like this:

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

You will have to do the allocation with malloc() , but outside of exercises almost always is what you would do. It is rare to create an array as an array in the stack and pass as a parameter. If it's just exercise do it the simplest way and use define , if not create the way it really can be used universally.

This form you do not want to use is only used in exercises. Much of everything you'll find on the internet, in books and in most sources is just exercise code, not how you actually program. But most people will not really program in C.

    
30.09.2018 / 16:38