Pointer address confusion

1
2
3
 int numbers[5] = {0, 1, 2, 3, 4};
       int *ptr = numbers;
       ptr++;                            


So i am still learning the main concept and uses of pointers ans I stumbled upon this code that I had a question about.

After the code executes, what address is ptr going to hold? it would be the address of numbers[1] because of the postfix incrementation? or would that only work with prefix?
Ptr's address will increment to 1. It is called pointer arithmetic.
If you need any additional help or explanation feel free to ask.
Ptr's address will increment to 1. It is called pointer arithmetic.

ptr's address will remain the same. Iow, &ptr prior to line 3 will be the same as &ptr after line 3.

The value held by ptr will be incremented to point to numbers[1], regardless of whether post- or pre-increment is used.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>

int main()
{
    int array[2] = { 0, 1 } ;

    for ( unsigned i=0; i<2; ++i )
        std::cout << "Address of array[" << i << "]:   " << &array[i] << '\n' ;

    int* ptr = array ;
    std::cout << "Address of ptr is      " << &ptr << '\n' ;
    std::cout << "Address held by ptr is " << ptr << '\n' ;

    ptr++ ;

    std::cout << "Address of ptr is      " << &ptr << '\n' ;
    std::cout << "Address held by ptr is " << ptr << '\n' ;
}
Address of array[0]:   0368F91C
Address of array[1]:   0368F920
Address of ptr is      0368F904
Address held by ptr is 0368F91C
Address of ptr is      0368F904
Address held by ptr is 0368F920


Sometimes a little experimenting can go a long way towards improving your understanding.
Yea thats what I meant lol.
Topic archived. No new replies allowed.