0
I am having problems in the cycle that stores the value of the variable aux in the first empty position of the vector re and I can’t see where my error is. The value being stored is not the value of aux, but the value -842150451
int l = 0, c = 0, o = 0, aux = 0;
int** mat = 0, *re = 0, reaux = 0;
printf("Ordem da matriz quadrada:\n>");
scanf_s("%i", &o);
l = o;
c = o;
reaux = l + c + 2;
re = malloc(sizeof(int*) * reaux);
//soma colunas
//ciclo para somar os valores
for (int i = 0; i < c; i++) {
for (int j = 0; j < l; j++) {
aux += mat[i][j];
}
}
//ciclo para armazenar no primeiro slot vazio
for (int k = 0; k < (l + c) - 1; k++) {
if (re[k] == 0) {
re[k] = aux;
aux = 0;
}
}
//ciclo para que caso a soma da coluna seja diferente da soma da coluna anterior a variável "cl" receba 1
for (int i = 0; i < c; i++) {
if ((int)i != 0 && re[(int)i] != re[(int)i - 1]) {
cl = 1;
}
}
Here: re = malloc(sizeof(int*) * Reaux); shouldn’t it be sizeof(int) and not sizeof(int*)? If you haven’t initialized your re array for what reason you test if the content is zero?
– anonimo
I initialized the array when I declared
*re = 0,, but I changed the malloc line to (int) as Voce suggested and the result was the same:-842150451.– Jordan
You did not initialize the array with zero, you assigned zero to the re pointer and after that you assigned re the address of the allocated area overwriting zero.
– anonimo
And how I fix it?
– Jordan
After allocating your array scan all your allocated positions by assigning zero to each position.
– anonimo
Also missing to allocate its matrix mat. Allocate an array of pointers (rows) and each of these pointers points to an allocated area of integers (columns).
– anonimo