user1427661
user1427661

Reputation: 11774

l value required as left operand of assignment

I'm trying to populate several arrays with the same randomly selected integer but I keep getting the error in the subject line. Here is my code:

while(i++ < arraySize){
        randInt = (int)random()%100;
        originalArray++ = randInt;
        ascendingOrderArray++ = randInt;
        descendingOrderArray++ = randInt;
    }

Why is this error occurring in this context? From my understanding this code is identical to assigning randInt to the three arrays and then incrementing their pointers at the end of the code.

Upvotes: 1

Views: 6766

Answers (3)

Ernest Friedman-Hill
Ernest Friedman-Hill

Reputation: 81694

Here, you are trying to assign a value to a number -- to the address of an array. A number is an rvalue, not an lvalue, so it fails.

originalArray++ = randInt;

Here, you are assigning a value to a memory location -- the address obtained by dereferencing a pointer. This is a lvalue, and so it succeeds:

*originalArray++ = randInt;

Upvotes: 1

autistic
autistic

Reputation: 15642

original++ is not an object, and so you can't store a value in it.

Upvotes: 0

user405725
user405725

Reputation:

Assuming that originalArray, ascendingOrderArray and descendingOrderArray are pointers of type int * and randInt is of type int, you have to de-reference the pointer when assigning. For example *originalArray++ = randInt;. That will de-reference the pointer yielding the "lvalue" you can assign to, assign the value of randInt to it (to whenver originalArray pointer points to), and increment the pointer afterwards.

Upvotes: 1

Related Questions