-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPointer.cpp
100 lines (83 loc) · 1.93 KB
/
Pointer.cpp
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include <iostream>
using namespace std;
void print(int *p) {
cout << *p << endl;
}
void update(int *p) {
*p = *p + 1;
}
void doublePointerUpdate(int **p) {
// p = p + 1;
// *p = *p + 1;
**p = **p + 1;
}
int main() {
int num = 5;
int *p = #
cout << *p << endl;
cout << p << endl;
cout << num << endl;
cout << &num << endl;
cout << &p << endl;
cout << sizeof(num) << endl;
cout << sizeof(&num) << endl;
cout << sizeof(*p) << endl;
cout << sizeof(p) << endl;
(*p)++;
cout << num << endl;
cout << *p << endl;
num++;
cout << num << endl;
cout << *p << endl;
cout << endl;
int arr[5] = {5, 4, 3, 2, 1};
cout << arr << endl;
cout << *arr << endl;
cout << &arr[0] << endl;
cout << &arr[1] << endl;
cout << &arr[2] << endl;
cout << *arr + 3 << endl;
cout << *(arr + 3) << endl;
cout << endl;
/* arr[i] = *(arr + i) */
/* i[arr] *(i + arr) */
int temp[10];
cout << sizeof(temp) << endl;
int *ptr = &temp[0];
cout << sizeof(ptr) << endl;
cout << endl;
int array[4] = {12, 3, 4, 5};
char ch[6] = "abcde";
/* will print address of first */
cout << array << endl;
/* print content abcde */
cout << ch << endl;
char *c = &ch[0];
cout << c << endl;
cout << endl;
int value = 5;
int *pointerrr = &value;
print(pointerrr);
print(&value);
cout << "Before: " << *pointerrr << endl;
update(pointerrr);
cout << "After: " << *pointerrr << endl;
cout << endl;
int i = 5;
int *pt = &i;
int **ptrr = &pt;
cout << i << endl;
cout << *pt << endl;
cout << **ptrr << endl;
cout << &i << endl;
cout << pt << endl;
cout << *ptrr << endl;
cout << endl;
cout << &pt << endl;
cout << ptrr << endl;
cout << &ptrr << endl;
cout << i << endl;
doublePointerUpdate(ptrr);
cout << i << endl;
return 0;
}