How can I visualize the concept of post-incrementing a pointer?
A pointer is a variable that points to a location in memory.
int *p开发者_StackOverflow中文版ointer1;
int *pointer2 = pointer1;
Let's say that both variables are pointing to memory location
0xA
Then I perform
pointer2++;
Now, pointer2
points to
0xB
Because both addresses point to the same place, I fully expect pointer1
to point to
0xB
But it does not. pointer1
still points to 0xA
.
How is this possible? Does the pointer have another address to specify which pointer it actually is? If yes, what is this second address called?
You are confusing the value stored in the pointer with the value to which the pointer points.
The two pointers, per se, are completely independent, they just happen to point to the same memory location. When you write
pointer2++;
you are incrementing the value stored in pointer2
(i.e. address to which it points), not the value stored at the pointed location; being pointer
and pointer2
independent variables, there's no reason why also pointer
should change its value.
More graphically:
int var=42;
int * ptr1 = &var;
int * ptr2 = ptr2;
Supposing that var
is stored at memory location 0x10, we'll have this situation:
+----------+
| | 0x00
+----------+
| | 0x04
+----------+
| | 0x08 +------------+
+----------+ +------| ptr1: 0x10 |
| | 0x0C | +------------+
+----------+ | +------------+
| var: 42 | 0x10 <----+------| ptr2: 0x10 |
+----------+ +------------+
| | 0x14
+----------+
| | 0x18
+----------+
| |
Now we increment ptr2
ptr2++;
(due to pointer arithmetic the stored address gets incremented of sizeof(int)
, that here we assume being 4)
+----------+
| | 0x00
+----------+
| | 0x04
+----------+
| | 0x08 +------------+
+----------+ +------| ptr1: 0x10 |
| | 0x0C | +------------+
+----------+ | +------------+
| var: 42 | 0x10 <----+ +--| ptr2: 0x14 |
+----------+ | +------------+
| | 0x14 <--------+
+----------+
| | 0x18
+----------+
| |
Now ptr2
points to 0x14 (what's there is not important in this example); ptr1
is left untouched pointing to 0x10.
(naturally both ptr1
and ptr2
have an address where they are stored, as any other variable; this is not shown in the diagrams for clarity)
Pointers, although having special characteristics, are numbers. In this respect, it's as if pointer1
and pointer2
were integers, which just happened to have the same value. If you increment one, you're not automatically incrementing the other.
pointer1
is an object that contains values of type int *
.
pointer2
is another object that also contains values of type int *
.
If you change the value contained in pointer2
there is no reason to think the value in pointer1
will also change. The exact same thing happens with values of any other type. For instance, with double
double a = 4.2;
double b = a;
b /= 10;
/* is `a` now 4.2, or 0.42?? */
In the line int *pointer2 = pointer1;
you are copying the address stored at pointer1 to pointer2. Both are independent variables. If you want to update the address stored at pointer1 through pointer2 you will need a pointer of pointer:
int **pointer2 = &pointer1;
(*pointer2)++;
精彩评论