pointer assignment
|
|
pointer assigment used a lot in linked list problems, the sample above is a pointer solution for linked list reverse. consider pointer as a container with an address to another object. pointer assignment, e.g. pointerA = pointerB only changes the content in the container. but the address of the container itself doesn’t change. and with *(dereference) the pointer, we can see the content is changed.
further, taken pc, pb, pd as another example.
the first line will make container pc to store what is stored in container pb, in another word, the first line will make pc point to the address, which is stored in pb.
and the second line will then put what’s stored in container pd to container pb.
after this two updates, pc points to the original content in pb; pb and pd points to the same content. obviously, what’s inside pc now, has nothing to do with pointer pb.
pointer++ forward
the basic scenario is as following, will p2 move forward as well ?
|
|
we can see from the following test.c, p2 won’t move forward as p1++.
|
|
pointer++ can be reviewed as a same type pointer in current pointer’s neighbor, then do a pointer assigment:
|
|