The type of matriz
is an integer pointer (array). In this case, when you have the operation matriz[i]
, the value you have is an integer. The problem is that you are trying to access the index [j]
of that entire value.
You need to declare the parameter matriz
as a array of arrays (or pointer pointer) to be able to access it via two indexes, as in the example below:
int contagemlinha(int ** matriz, int L, int C) {
int i, j, soma = 0;
for (i = 0; i < L; i++)
for (j = 0; j < C; j++)
soma += matrix[i][j];
return soma;
}
Another way to store two-dimensional matrices is to store it in a simple array, with the elements of the first line followed by the elements of the second, and so on. For example, the matrix
1 2 3 4
5 6 7 8
9 10 11 12
Can be stored in the array
1 2 3 4 5 6 7 8 9 10 11 12
If that’s your case, then you just need to scroll through the matrix with an index:
int contagemlinha(int* matriz, int N)
{
int i, soma = 0;
for(i = 0; i < N; i++)
soma += matriz[i];
return soma;
}
No need to increase
i
also?i++;
belowsoma += ..
.– stderr