postfix in array index
I am trying to convert some c++ to another more basic language and i just wanted to clarify something.
Does this code increment the array index before or after it is used.
Like if i did the following:
ind = 3
a = array[ind++]
would a be assigned the value in element 3 or element 4?
I am still learning c++ and have never seen this before so any comments would be much appreciated.
a would be assigned the value in element 3. ind would only then be incrememented.
However... it important to understand one thing; When is ind really incremented?
You have to understand what lvalues and rvalues are.
Shortly, an lvalue is an expression that can appear both in the left and the right side of an assigment. While a rvalue can only appear on the right-hand side of an assignment.
So the big difference is this...
int a = 12; // a is an lvalue. 12 is a rvalue. You cannot write 12 = a;
int b = a; // b and a are lvalues. a, being an lvalue, can also be used on the right-hand side.
int c = a + b * 12; // The expression a + b * 12 yields a rvalue. You cannot write a + b *12 = c;
A prefixed increment or decrement operator (++ind) yields a lvalue as a result. It returns the object itself after applying the operator.
A postfixes increment or decrement operator (ind++) yields a rvalue. It returns a copy of the unchanged value of the object before it was incremented.
Both prefix and suffix versions of ind you have, happen when ind is being evaluated. However what is really different is what is returned from them.
Thanks for such a quick and detailed explanation, itís got me out of a hole I had been wallowing in for some time :0)