Now that you gave us the code we can answer your question.
So the confusing piece is this: &arr + i
. This does not do what you think it does. Remember that &
takes precedence over +
. And so you take address of arr
and move it forward i
times.
Pointer arithmetic works in such a way that &x + 1
moves the pointer forward by size(x)
. So in your case what is size(arr)
? It is 8 because it is 2-element array of integers (I'm assuming ints are of size 4). And so &arr + 1
actually moves the pointer 8 bytes forward. The exact thing you experience. You don't ask for next int, you ask for next array. I encourage you to play around, for example define arr
as int[3]
(which is of size 12) and see how the pointer moves 12 bytes forward.
So first solution is to do arr + i
without &
. We can apply pointer arithmetic to an array in which case it decays to a pointer type int*
. Now since int
is of size 4 then arr + 1
points correctly to a memory segment 4 bytes forward.
But what I suggest is to stay away from pointer arithmetic and do &arr[i]
instead. This does the same thing but IMO is less error prone, less confusing and tells us more about the intent.