-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpointer.cpp
95 lines (75 loc) · 1.73 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
#include<bits/stdc++.h>
using namespace std;
void f(int* z) {
*z = 25;
}
void fref(int& z) {
z = 25;
}
void sort(int *l, int* r) {
}
int z[10];
int* g() {
int* t = (int*)malloc(10*sizeof(int));
for (int i = 0; i < 10; i++) t[i] = i*i;
return t;
}
/// O(v.size())
void fv(vector<int> v) {
}
/// O(1)
void fvref(vector<int> &v) {
}
/// O(1)
void fvpnt(vector<int> *v) {
}
void printArray(int* a, int n) {
for (int i = 0; i < n; i++)
cout << a[i] << ' ';
cout << "\n----\n";
}
int main() {
int x = 10;
fref(x);
cout << x << '\n';
cout << "Value x: " << x << '\n';
cout << "Address x: " << (&x) << '\n';
int* p2x = &x;
cout << "Address of pointed address: " << (p2x) << '\n';
cout << "Address of p2x: " << (&p2x) << '\n';
cout << "Value of p2x in ram: " << (*p2x) << '\n';
x = 15;
cout << "New value of x:" << (*p2x) << '\n'; // 15
int** p2p2x = &p2x;
cout << "Address of p2p2px in ram: " << (&p2p2x) << '\n';
cout << "p2x: " << (*p2p2x) << '\n';
cout << "*p2x == x: " << (**p2p2x) << '\n';
*p2x = 20;
cout << x << '\n';
f(p2x);
cout << x << '\n';
int a[10];
a[3] = 10;
cout << (a) << '\n';
/// a is pointer to first element of array
cout << a[3] << '\n'; /// 10
cout << (*(a+3)) << '\n'; /// 10
sort(a, a+10);
int* int_p = &x;
cout << int_p << '\n';
cout << (int_p+1) << '\n';
bool b = false;
bool* bool_p = &b;
cout << bool_p << '\n';
cout << (bool_p + 1) << '\n';
char ch[5];
ch[0] = '1';
ch[1] = 'x';
ch[2] = '-';
cout << (ch) << '\n'; /// 1x-
int* c = g();
cout << (*c) << ' ' << (*(c+1)) << '\n';
printArray(a, 10);
int* pn = NULL;
pn = &x;
}