A
A
akivi282022-02-24 01:07:07
C++ / C#
akivi28, 2022-02-24 01:07:07

Why is the compiler expecting a pointer in my code?

In this code, I got an error: Expression must be of type pointer to object, but is of type "int".

void IzmerenieOsadkov(int arr[], int size1, int size2)
{
    for (int i = 0; i < size1; i++)
    {
        int sum = 0, max = 0, index = 0;
        for (int j = 0; j < size2; j++)
        {
            sum += arr[i][j]; // подчоркнута j и ошибка Выражение должно иметь тип указателя на объект, но имеет тип "int"
        }

        if (sum > max)
        {
            max = sum;
            index = i;
        }
            
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
W
Wataru, 2022-02-24
@wataru

Function parameter int arr[],
But then you have arr[i][j]. The compiler tries to take the index j, but there is already a number in front of it. So he tells you "chief, I can take the index from the array (pointer), but here the number is not clear."
You are treating a one-dimensional array as if it were two-dimensional.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question