question help on pointers

I can't wrap my head around this one. Can you guys simplify this concept for me so I can understand easier? What would the output here be? I tried running this code and I get 5197373. But my answer choices are:

a. Five memory addresses
b. 0
c. 3
d. 2
3. 1

I instinctively thought my answer was going to be the address of the numbers[2] element, but that's not an option. What am I not understanding here?

1
2
3
4
    int *numbers = new int[5];
    for (int i=0; i <=4; i++)
        *(numbers+1) = i;
    cout << numbers[2] << endl;
In the loop numbers[1] ( numbers + 1 )is assigned i. As the last value of i is equal to 4 then numbers[1] will be equal to 4. However after the loop you are displying numbers[2] that was not assigned any value. So an arbitrary value is displayed.
So basically this loop is ONLY manipulating the numbers[1] element as it iterates. The cout statement is not affected by the loop at all. Now if I were to change it to this below, then my output would be 4. Is that correct?

 
*(numbers+2) = i;
You are right, the output wilol be 4.
great. thanks Vlad!
Topic archived. No new replies allowed.