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);
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
– Filipe
@Calm Philip, anything put there.
– gfleck
So, I tried to do it there, but the program stops working, hangs
– Filipe
I’ll update the question for you to see
– Filipe
@Filipe Cara, I couldn’t identify the error, you can debug your program and see which line is crashing?
– gfleck
I did some research, and it seems there’s no way to pass matrix values to matrix, just by pointer
– Filipe
@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.
– gfleck
@Filipe Conseguiu?
– gfleck