I'm learning about iterators and operator precedence but I've run into this confusing problem. I can't seem to figure why the dereference operator is not taking precedence between these two:
++*iter
This one changes the value at which the iterator points to without changing the iter up.
*++iter
This seems to move the iterator up without changing any element values.
Is this because the two are considered "right-grouped" operators and so they combine like: *(++iter) ?
"Expressions with unary operators group right-to-left."
So expression
++*iter
means that at first you get the value pointed by the iter and then increase the value.
Expression
*++iter
means that at first you increase the iterator (so it will point to the next element in the sequence) and only then you get the value of the incremented iterator.
Thanks for the answer by the way. I wrote a bunch of code that would help me understand what was going on by printing memory addresses for objects and their elements and came upon this little oddity:
address of cp: 123
address of 1: 0x7fff5fbff758
address of 2: 0x7fff5fbff748
address of 3: 0x7fff5fbff740
It seems weird to me that the memory addresses would move backwards rather than forward but I bet it's normal. Any idea what causes this and what I should read up on to understand it?
That makes a lot of sense. I mistakenly thought it was printing the address to the individual element. I'm going to look over the code you pasted now.
/edit
Just wanted to say without bumping up this thread that things have become more clear. I realize now that static variables will go on the heap as opposed to the stack, which explains why they're being addressed in very different spaces in your example code.
Then because the stack grows downward, the variables I was declaring will go from a high address to a low one.
Thanks again for your help. That link you sent me is a goldmine.