-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCpp1_7.html
More file actions
89 lines (75 loc) · 3.23 KB
/
Copy pathCpp1_7.html
File metadata and controls
89 lines (75 loc) · 3.23 KB
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
<article>
<h1>C++ I: Page 7 - Pointers & References</h1>
<section>
<h2>Direct Memory Control</h2>
<p>Pointers and References are the foundational tools that give C++ its power and performance, but they are also the primary source of bugs. Mastering them is essential.</p>
<h3>The Code implementation</h3>
<pre><code class="language-cpp">/*
* File: pointers.cpp
* Demonstration of pointers, references, and const correctness
*/
#include <iostream>
using namespace std;
void modifyWithPointer(int* p) {
if (p != nullptr) {
*p = *p * 2; // Dereference pointer to modify value
}
}
void modifyWithReference(int& r) {
r = r * 3; // Reference acts as an alias
}
int main() {
int value = 10;
// 1. Pointer Basics
int* ptr = &value; // Holds address of value
cout << "Value: " << value << ", Pointer: " << *ptr << endl;
modifyWithPointer(ptr);
cout << "After Pointer Mod: " << value << endl;
// 2. Reference Basics
int& ref = value; // Alias to value
modifyWithReference(ref);
cout << "After Reference Mod: " << value << endl;
// 3. Pointer Arithmetic
int arr[] = {1, 2, 3};
int* pArr = arr;
cout << "Array item 1: " << *(pArr + 1) << endl;
// 4. Const Correctness
const int cVal = 100;
const int* p1 = &cVal; // Pointer to const int (data is const)
int* const p2 = &value; // Const pointer to int (pointer is const)
const int* const p3 = &cVal; // Const pointer to const int
cout << "Const Correctness: " << *p1 << endl;
return 0;
}
/*
* Pointers vs. References Nuances:
* 1. Pointer: Holds address, can be NULL, can be re-assigned.
* 2. Reference: Alias, cannot be NULL, must be initialized, cannot be re-bound.
* 3. Nullptr: Use 'nullptr' instead of 'NULL' in modern C++.
* 4. Pointers allow arithmetic (p+1); References do not.
* 5. Pass by reference is generally preferred for large objects to
* avoid unnecessary copying (Pass-by-value).
* 6. 'const' with pointers is crucial:
* - const int* p (data is immutable)
* - int* const p (pointer address is immutable)
* 7. Pointers provide more flexibility, while references provide safer
* syntax for aliasing existing objects.
*/
</code></pre>
<div class="interactive-sim">
<h3>Pointer Simulator</h3>
<button class="sim-button" onclick="
const out = document.getElementById('sim-output-cpp1_7');
out.innerHTML = 'int x = 10; int* p = &x;<br>';
setTimeout(() => out.innerHTML += '>> *p = 20;<br>>> x is now 20', 800);
">Run Simulation</button>
<div id="sim-output-cpp1_7" class="sim-output"></div>
</div>
</section>
<footer style="margin-top: 40px; padding: 20px; background: #f8f9fa; border-top: 1px solid #dee2e6;">
<div style="display: flex; justify-content: space-between;">
<a href="#" data-file="Cpp1_6.html" style="text-decoration: none; color: #6c757d;">← Previous: Memory: Stack vs Heap</a>
<a href="#" data-file="Cpp1_8.html" style="font-weight: bold; text-decoration: none; color: #007bff;">Next: OOP: Classes & Objects →</a>
</div>
</footer>
</article>