How can I visualize the concept of post-incrementing pointer?

A pointer is a variable indicating a location in memory.

int *pointer1; int *pointer2 = pointer1; 

Say both variables indicate memory location

 0xA 

Then i do

 pointer2++; 

Now pointer2 points to

 0xB 

Since both addresses point to the same place, I fully expect pointer1 point to

 0xB 

But this is not so. pointer1 is still pointing to 0xA .

How is this possible? Does the pointer have another address to indicate which pointer it really is? If so, what is the second address called?

+4
source share
4 answers

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)

+6
source

Pointers, although they have special characteristics, are numbers. In this regard, it is as if pointer1 and pointer2 were integers, which, as it turned out, had the same value. If you enlarge one, you will not automatically enlarge the other.

+2
source

pointer1 is an object that contains int * values.
pointer2 is another object that also contains int * values.

If you change the value contained in pointer2 , there is no reason to think that the value in pointer1 will also change. The same thing happens with values โ€‹โ€‹of any other type. For example, when double

 double a = 4.2; double b = a; b /= 10; /* is `a` now 4.2, or 0.42?? */ 
+2
source

In the line int *pointer2 = pointer1; you copy the address stored in pointer1 to pointer2. Both are independent variables. If you want to update the address stored in pointer1 through pointer2, you will need a pointer pointer:

 int **pointer2 = &pointer1; (*pointer2)++; 
0
source

All Articles