pure c/c++ pointer

pointer assignment

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
int main(){
int a = 10 ;
int *pa = &a ;
cout << "pa add " << &pa << "pa val " << *pa << endl ;
int b = 22;
int *pb = &b ;
cout << "pb add " << &pb << "pb val " << *pb << endl ;
int d = 100 ;
int *pd = &d ;
cout << "pd add " << &pd << "pd val " << *pd << endl ;
int *pc = pb ; //pointer assignment
cout << "pc add " << &pc << "pc val " << *pc << endl ;
pb = pd ;
cout << "pb add " << &pb << "pb val " << *pb << endl ;
cout << "pd add " << &pd << "pd val " << *pd << endl ;
pd = pa ;
cout << "pd add " << &pd << "pd val " << *pd << endl ;
pa = pc ;
cout << "pa add " << &pa << "pa val " << *pa << endl ;
cout << "pc add " << &pc << "pc val " << *pc << endl ;
return 0;
}
/*
pb add 0x7fff5e744b30pb val 22
pd add 0x7fff5e744b28pd val 100
pc add 0x7fff5e744b20pc val 22
pb add 0x7fff5e744b30pb val 100
pd add 0x7fff5e744b28pd val 100
pd add 0x7fff5e744b28pd val 10
pa add 0x7fff5e744b40pa val 22
pc add 0x7fff5e744b20pc val 22
*/

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.

1
2
pc = pb ;
pb = pd;

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 ?

1
2
3
int *p1 = &int_var ;
int *p2 = p1;
p1++ ;

we can see from the following test.c, p2 won’t move forward as p1++.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
int main(){
int a[4] = {1, 2, 3, 4 } ;
cout << " a addr " << &a << " a val " << *a << endl ;
int *p = a ;
int *q = p ;
cout << " p addr " << &p << " p val " << *p << endl ;
cout << " q addr " << &q << " q val " << *q << endl ;
for(int i=0; i<3; i++){
q++;
}
cout << " a addr " << &a << " a val " << *a << endl ;
cout << " p addr " << &p << " p val " << *p << endl ;
cout << " q addr " << &q << " q val " << *q << endl ;
return 0;
}
/*
a addr 0x7fff5e968bb0 a val 1
p addr 0x7fff5e968b40 p val 1
q addr 0x7fff5e968b38 q val 1
a addr 0x7fff5e968bb0 a val 1
p addr 0x7fff5e968b40 p val 1
q addr 0x7fff5e968b38 q val 4
*/

pointer++ can be reviewed as a same type pointer in current pointer’s neighbor, then do a pointer assigment:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int* tmp_p = 0x7fff5e744b30 ;
int* p1 = 0x7fff5e744b28 ;
p1 = tmp_p ;
```
### int++
```c++
int main(){
vector<int> nums ;
for(int i=0; i<5; i++){
nums.push_back(i);
}
int p=0;
std::cout << "nums[p++] " << nums[p++] << " p val " << p << endl ;
return 0;
}
/* nums[p++]: 0 p val: 1 */