2
In the execution I am developing I try to pass a matrix created dynamically created with the function malloc
, but in doing so the compiler points to type incompatible with pointer.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
void substituiNegativos(int c, int mat[][c]){
int i, j;
for(i=0; i< 2; i++){
for(j=0; j< 2; j++){
if(mat[i][j] < 0){
mat[i][j] = abs(mat[i][j]);
}
}
}
for(i=0; i< 2; i++){
for(j=0; j<2; j++){
printf("%d ", mat[i][j]);
}
printf("\n");
}
}
int main()
{
int i, j, l, c;
printf("Numero de linhas: ");
scanf("%d", &l);
printf("Numero de colunas: ");
scanf("%d", &c);
int **mat = (int**)malloc(l * sizeof(int));
for(i=0; i<l; i++){
mat[i] = (int*)malloc(c * sizeof(int));
for(j=0; j<c; j++){
printf("Insira o numero: ");
scanf("%d", &mat[i][j]);
}
}
substituiNegativos(c, mat);
return 0;
}
I appreciate the explanation, it worked, but I would also like to know more out of curiosity, if I could pass the argument this way
void substituiNegativos(int c, int mat[][c])
, being the variablec
my column size, or C does not allow it. I thank you already.– jaojpaulo
Not allowed for two reasons: 1 - Its variable 'c' is local within the main function. It is only seen inside main. 2 - 'c' does not represent any value in the scope of the'void substituent(int c, int mat[][c])'. As I had explained, it is necessary to declare a value in the matrix column in a function scope
– Roger
Thanks, it helped a lot.
– jaojpaulo