Error : subscripted value is neither array nor Pointer nor vector

Asked

Viewed 9,872 times

0

Hello, good afternoon, I’m trying to get the values that are in my matrix in the function, but this giving this problem:1:19: error: subscripted value is neither array nor Pointer nor vector

Below is the function code:

int contagemlinha(int* matriz, int N)
{
    int i, j, soma;

    for(i = 0, j = 0; j < N; j++)
    soma += matriz[i][j];

    return soma;
}

is it wrong to try to allocate in an integer of int these values?? grateful

  • No need to increase i also? i++; below soma += ...

1 answer

1


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

Browser other questions tagged

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