Error in array as function parameter

1

Good evening.

I'm having a problem using an array as a parameter in my functions. I have a function that has an int array as a parameter, when I call the array an original array, and in the function I manipulate the parameter and saved in an external file, the problem is in the manipulation part, when I manipulate the parameter, the values of the original matrix are being modified too, and it is not for them to be modified, it is for only the value of the parameter matrix being modified. It may have been a bit confusing but I believe that seeing the code will understand:

Part where I create the original array:

FILE *pgm;
do
{
    char arq[30];
    printf("Digite o nome de arquivo para leitura com .pgm no fim(ex: imagem.pgm): ");
    setbuf(stdin, NULL);
    gets(arq);
    pgm = fopen(arq, "r");
}while(pgm == NULL);

char tipo[5];
fscanf(pgm, "%s", &tipo);
fscanf(pgm, "%d %d", &col, &lin);
fscanf(pgm, "%d", &escala);
int img[lin][col];
for(i = 0; i < lin; i++)
{
    for(j = 0; j < col; j++)
    {
        fscanf(pgm, "%d", &img[i][j]);
    }
}

fclose(pgm);

Function that uses an array as a parameter:

void escurerClarear(char tipo[], int mat[lin][col]){

float mult;
system("cls");
printf("Informacoes:\n\nPara clarear/escurecer e necessario fornecer um multiplicador com ou sem casa decimal\n\nEsse multiplicador deve ser maior que 0\n\nUm valor abaixo de 1 escurece a imagem e acima de 1 clareia\n\nDigite um multiplicador: ");
scanf("%f", &mult);
while(mult < 0)
{
    printf("\n\nO multiplicador deve possuir valor positivo: ");
    scanf("%f", &mult);
}
for(i = 0; i < lin; i++)
{
    for(j = 0; j < col; j++)
    {
        if((mult * mat[i][j]) > escala)
        {
            mat[i][j] = escala;
        }
        else
        {
            mat[i][j] = mult * mat[i][j]);
        }
    }
}

salvarImg(tipo, mat);}

Calling the function:

escurerClarear(tipo, img);

After executing the function the values of the img array are being changed and I do not want modificalos, I want to modify only the values of the mat matrix of the

EDITION ::

int temp[lin][col];
for(i = 0; i < lin; i++)
            {
                for(j = 0; j < col; j++)
                {
                    temp[i][j] = img[i][j];
                }
            }
            escurerClarear(tipo, temp);
    
asked by anonymous 11.08.2017 / 04:51

1 answer

0
When we pass an array to a function we are actually passing the memory address of the first element of the array, so it is passing a parameter by reference and the values will be changed within the function that was called ( escurerClarear ).

What you'll have to do is create another two-dimensional array of the same size as your original array, then copy the data from the original array to the newly created array, and then move it to the function.

Before calling your function, escurerClarear copies the data from the original array to another array with a for loop:

for(i = 0; i < lin; i++)
    for(j = 0; j < col; j++)
        matriz_temporaria[i][j] = matriz_original[i][j];

After this, pass the variable matriz_temporaria in the same way that the original matrix passed and be happy.

altera_matriz(matriz_temporaria);

Or if you prefer to do this within your% s function, even whichever you like best.

Here's an example of a little program I've done for you to understand better:

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

const int lin = 3;
const int col = 3;

int altera_matriz(int matriz[][col]);

int main ()
{
    int i, j;

    int matriz_original[lin][col];
    int matriz_temporaria[lin][col];

    for(i = 0; i < lin; i++)
        for(j = 0; j < col; j++)
            matriz_original[i][j] = j;

    for(i = 0; i < lin; i++)
        for(j = 0; j < col; j++)
            matriz_temporaria[i][j] = matriz_original[i][j];

    altera_matriz(matriz_temporaria);

    for(i = 0; i < lin; i++)
        for(j = 0; j < col; j++)
            printf("matriz_original[%i][%i]:%i \n", i, j, matriz_original[i][j]);

    for(i = 0; i < lin; i++)
        for(j = 0; j < col; j++)
            printf("matriz_temporaria[%i][%i]:%i \n", i, j, matriz_temporaria[i][j]);

    return 0;
}

int altera_matriz(int matriz[][col])
{
    matriz[0][0] = 0;
    matriz[0][1] = 0;
    matriz[0][2] = 0;

    matriz[1][0] = 0;
    matriz[1][1] = 0;
    matriz[1][2] = 0;

    matriz[2][0] = 0;
    matriz[2][1] = 0;
    matriz[2][2] = 0;

    return 0;
}
    
11.08.2017 / 09:45