-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathREFERENCES AND POINTERS.cpp
118 lines (70 loc) · 1.31 KB
/
REFERENCES AND POINTERS.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
//1/9: Introduction
//2/9: References
#include <iostream>
int main() {
int soda = 99;
int &pop = soda;
pop += 1;
std::cout << soda << "\n" << pop;
}
//3/9: Pass-By-Reference
#include <iostream>
int triple(int &i) {
i = i * 3;
return i;
}
int main() {
int num = 1;
std::cout << triple(num) << "\n";
std::cout << triple(num) << "\n";
}
//4/9: Pass-By-Reference with Const
#include <iostream>
int square(int const &i) {
return i * i;
}
int main() {
int side = 5;
std::cout << square(side) << "\n";
}
//5/9: Memory Address
#include <iostream>
int main() {
int power = 9000;
// Print power
std::cout << power << "\n";
// Print &power
std::cout << &power;
}
//6/9: Pointers
#include <iostream>
int main() {
int power = 9000;
// Create pointer
int* ptr = &power;
// Print ptr
std::cout << ptr;
}
//7/9: Dereference
#include <iostream>
int main() {
int power = 9000;
// Create pointer
int* ptr = &power;
// Print ptr
std::cout << ptr << "\n";
// Print *ptr
std::cout << *ptr;
}
//8/9: Null Pointer
#include <iostream>
int main() {
int power = 9000;
// Create pointer
int* ptr = nullptr;
// Later in the program...
ptr = &power;
// Print ptr
std::cout << ptr << "\n";
}
//9/9: Review