Matrix error as function parameter

Asked

Viewed 127 times

1

Good night.

I have a problem using an array as a parameter in my functions. I have in a function that has an int matrix as parameter, when I call the matrix step an original matrix, and in the function 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 as well, and they are not to be modified, so only the value of the parameter matrix is being modified. It may have gotten a little confusing but I think seeing the code will understand:

Part where I create the original matrix:

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 using a matrix 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 matrix are being exchanged and do not want to modify them, I want to modify only the value of the mat matrix, of the parameter

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);

1 answer

0

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

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

Before I call your function escurerClarear copies the data from the original matrix to another matrix 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 that pass the variable matriz_temporaria in the same way that it passed the original matrix and be happy.

altera_matriz(matriz_temporaria);

Or if you prefer to do it within your function escurerClarear same, whatever you think best.

Here is an example of a small program that I made to Voce better understand:

#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;
}
  • I imagined you something similar, because I’ve seen functions that passed the matrix as pointer , example *img, I’ll try to do what you said

  • @Calm Philip, anything put there.

  • So, I tried to do it there, but the program stops working, hangs

  • I’ll update the question for you to see

  • @Filipe Cara, I couldn’t identify the error, you can debug your program and see which line is crashing?

  • I did some research, and it seems there’s no way to pass matrix values to matrix, just by pointer

  • @Filipe Take a look at my answer, I edited it. Now I think Voce will understand better. Compile that little program I made to see that it works well.

  • @Filipe Conseguiu?

Show 3 more comments

Browser other questions tagged

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