问题描述:

Its giving me this error for line *(repetitions + x)++; inside this for loop. Any clues why?

for (int y = 0; y<hours; y++)

{

if (*(array + x) == *(array + y))

{

*(repetitions + x)++;

}

}

网友答案:

You can't increment the rvalue repetitions + x. This is the same error as writing:

int a = 3;
int b = 2;
(a+b)++;     // ????

The ++ operator requires an lvalue, i.e. the designation of a variable. a+b is a temporary result and doesn't have a memory address, it can't be incremented.

It's possible that you meant to write (*(repetitions + x))++;, which could be more clearly expressed as repetitions[x]++;

网友答案:

This parses as *((repetitions + x)++) -- that is, it tries to modify a constant address, then dereference it. Presumably you intended increment what the address pointed at instead.

You can do that a couple different ways. One would be to use parentheses. Another to use pre-increment:

++*(repetitions + x);

or:

(*(repetitions + x))++;
相关阅读:
Top