Error: subscripted value is neither array nor pointer nor vector

0

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

Here is the code for the function:

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

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

    return soma;
}

Wrong attempts to allocate an int integer to these values? grateful

    
asked by anonymous 02.06.2015 / 18:30

1 answer

1

The type of matriz is a pointer (array) of integers. In this case, when you have the matriz[i] operation, the value you have is an integer. The problem is that you are trying to access the index [j] of this integer value.

You need to declare the matriz parameter as an arrays array (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 arrays is to store it in a simple array, with elements of the first line followed by elements of the second, and so on. For example, the array

1  2  3  4
5  6  7  8
9 10 11 12

Can be stored in array

1  2  3  4  5  6  7  8  9 10 11 12

If this is your case then you just have to go through the array with an index:

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

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

    return soma;
}
    
02.06.2015 / 18:40