Passing array to function by reference

0

Good morning everyone, I want in my code to leave the Main (); only with function call, all work is divided into small functions in the document.

I want to write an array [3] [3] in a function and organize and print it in other distinct functions. My code is to transpose an array, I believe my logic is correct, using bubble sort however the parameter pass is not working correctly since I am using pass-by-value and non-reference ...

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

void receberMatriz(int matriz[3][3])
{
    int i, j;

    for(i = 0; i < 3; i++)
    {
        for(j = 0; j < 3; j++)
        {
            printf("Insira o valor de [%i][%i]: ", i, j);
            scanf("%i", &matriz[i][j]);
        }
    }

}



void organizarMatriz(int matriz[3][3])
{
    int i, j, aux;

    for(i = 0; i < 3; i++)
    {
        for(j = 0; j < 3; j++)
        {
            aux = matriz[i][j];
            matriz[i][j] = matriz[j][i];
            matriz[j][i] = aux;
        }
    }
}

void imprimirMatriz(int matriz[3][3])
{
    int i, j;

    for(i = 0; i < 3; i++)
    {
        for(j = 0; j < 3; j++)
        {
            printf("[%i][%i]", i, j);
        }

        printf("\n");

    }
}



    int main(void)
    {
        int matriz[3][3];

        receberMatriz(matriz);
        imprimirMatriz(matriz);
        //organizarMatriz(matriz); desabilitei chamada para testar valores recebidos e impressos pela matriz. 

        return 0;
    }

How can I work with passing parameters from an array by reference? Or pointers ...?

    
asked by anonymous 20.12.2017 / 13:18

1 answer

1

If someone has a similar question I will leave the answer. The logic bug that our developer Maniero commented is the following:

I changed the balls by writing the matrix print function.

void imprimirMatriz(int matriz[3][3])
{
    int i, j;

    for(i = 0; i < 3; i++)
    {
        for(j = 0; j < 3; j++)
        {
            printf("[%i][%i]", i, j);
        }

        printf("\n");

    }
}

No 'printf ("[% i] [% i]", i, j); besides putting two output of integer data I was printing the index value and not the array. The correct code is the following:

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

void receberMatriz(int matriz[3][3])
{
    int i, j;

    for(i = 0; i < 3; i++)
    {
        for(j = 0; j < 3; j++)
        {
            printf("Insira o valor de [%i][%i]: ", i, j);
            scanf("%i", &matriz[i][j]);
        }
    }

}



void imprimirMatriz(int matriz[3][3])
{
    int i, j;

    printf("\n");
    limparTela();

    for(i = 0; i < 3; i++)
    {
        for(j = 0; j < 3; j++)
        {
            printf("[ %i ]", matriz[i][j]);
        }

        printf("\n");

    }
}



void limparTela()
{
    system("cls");
}



    int main(void)
    {
        int matriz[3][3];

        receberMatriz(matriz);
        imprimirMatriz(matriz);

        return 0;
    }

Credits at Maniero

    
20.12.2017 / 14:22