Reputation: 439
What is the meaning of "=" when you try to switch a pointer.
for example: current->next = previous
and current = previous
.
Upvotes: 1
Views: 102
Reputation: 4673
Let's take the second assignment, current = previous
. Here, the small boxes are the values of the pointers themselves, while the rectangles on the right are the things they point to.
Before assignment:
+---+ +-----------+
current | --|--------> | Obj1 |
+---+ +-----------+
+---+ +-----------+
previous | --|--------> | Obj2 |
+---+ +-----------+
After assignment, both current
and previous
point to the object Obj2.
+---+ +-----------+
current | --|--+ | Obj1 |
+---+ | +-----------+
|
+---+ +-----> +-----------+
previous | --|--------> | Obj2 |
+---+ +-----------+
Important takeaways:
current
used to point to is unchanged. It is not "deleted" or destructed by virtue of the assignment.Obj1
nor Obj2
themselves.*current = func()
), then its changed state will be reflected when you access (read) it through either current
or previous
.Upvotes: 4