You confuse the value stored in the pointer with the value that the pointer points to.
The two pointers are essentially completely independent; they simply point to the same memory location. When you write
pointer2++;
You increase the value stored in pointer2 (that is, the address that it points to), and not the value stored in the specified location; being pointer and pointer2 independent variables, there is no reason why pointer should also change its value.
More clearly:
int var=42; int * ptr1 = &var; int * ptr2 = ptr2;
Suppose var is stored in memory 0x10, we will have this situation:
+----------+ | | 0x00 +----------+ | | 0x04 +----------+ | | 0x08 +------------+ +----------+ +------| ptr1: 0x10 | | | 0x0C | +------------+ +----------+ | +------------+ | var: 42 | 0x10 <----+------| ptr2: 0x10 | +----------+ +------------+ | | 0x14 +----------+ | | 0x18 +----------+ | |
Now we increase ptr2
ptr2++;
(due to pointer arithmetic, the stored address increases with sizeof(int) , here we assume that it is 4)
+----------+ | | 0x00 +----------+ | | 0x04 +----------+ | | 0x08 +------------+ +----------+ +------| ptr1: 0x10 | | | 0x0C | +------------+ +----------+ | +------------+ | var: 42 | 0x10 <----+ +--| ptr2: 0x14 | +----------+ | +------------+ | | 0x14 <--------+ +----------+ | | 0x18 +----------+ | |
Now ptr2 points to 0x14 (which is not important in this example); ptr1 remains untouched, indicating 0x10.
(naturally, both ptr1 and ptr2 have an address where they are stored, like any other variable, this is not shown in the diagrams for clarity)
source share