Pointers vs References in C++: Behavior, Syntax, and Use Cases
What is Pointers vs References in C++: Behavior, Syntax, and Use Cases? Pointers and references are two important features of C++ that allow programmers to access and work with existing objects indirectly.Both can be used to modify...
What are the architectural and syntax differences between pointers and references in C++?
Explanation & In-Depth Details
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code>#<span class="js-id">include</span> <span class="js-op"><</span><span class="js-id">iostream</span><span class="js-op">></span>
<span class="js-id">using</span> <span class="js-id">namespace</span> <span class="js-id">std</span><span class="js-op">;</span>
<span class="js-id">int</span> <span class="js-id">main</span><span class="js-op">()</span> <span class="js-op">{</span>
<span class="js-id">int</span> <span class="js-id">number</span> <span class="js-op">=</span> <span class="js-num">100</span><span class="js-op">;</span>
<span class="js-id">int</span><span class="js-op">*</span> <span class="js-id">ptr</span> <span class="js-op">=</span> <span class="js-op">&</span><span class="js-id">number</span><span class="js-op">;</span>
<span class="js-id">cout</span> <span class="js-op"><<</span> <span class="js-str">"Original value: "</span> <span class="js-op"><<</span> <span class="js-id">number</span> <span class="js-op"><<</span> <span class="js-id">endl</span><span class="js-op">;</span>
<span class="js-id">cout</span> <span class="js-op"><<</span> <span class="js-str">"Memory address: "</span> <span class="js-op"><<</span> <span class="js-id">ptr</span> <span class="js-op"><<</span> <span class="js-id">endl</span><span class="js-op">;</span>
<span class="js-id">cout</span> <span class="js-op"><<</span> <span class="js-str">"Value through pointer: "</span> <span class="js-op"><<</span> <span class="js-op">*</span><span class="js-id">ptr</span> <span class="js-op"><<</span> <span class="js-id">endl</span><span class="js-op">;</span>
<span class="js-kw">return</span> <span class="js-num">0</span><span class="js-op">;</span>
<span class="js-op">}</span></code></pre></div><p><strong>Example output:</strong></p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-id">Original</span> <span class="js-id">value</span><span class="js-op">:</span> <span class="js-num">100</span>
<span class="js-id">Memory</span> <span class="js-id">address</span><span class="js-op">:</span> 0x7ffe1234
<span class="js-id">Value</span> <span class="js-id">through</span> <span class="js-id">pointer</span><span class="js-op">:</span> <span class="js-num">100</span></code></pre></div><p><em>The displayed memory address is illustrative. Actual addresses vary between systems and program executions.</em></p><h3>Explanation</h3><ul><li><p><code>number</code> stores the integer value <code>100</code>.</p></li><li><p><code>&number</code> obtains the address of the variable.</p></li><li><p><code>ptr</code> stores that address.</p></li><li><p><code>*ptr</code> accesses the value at the stored address.</p></li></ul><h3>Pointer syntax</h3><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-id">data_type</span><span class="js-op">*</span> <span class="js-id">pointer_name</span> <span class="js-op">=</span> <span class="js-op">&</span><span class="js-id">variable_name</span><span class="js-op">;</span></code></pre></div><p>Example:</p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-id">int</span> <span class="js-id">age</span> <span class="js-op">=</span> <span class="js-num">25</span><span class="js-op">;</span>
<span class="js-id">int</span><span class="js-op">*</span> <span class="js-id">ptr</span> <span class="js-op">=</span> <span class="js-op">&</span><span class="js-id">age</span><span class="js-op">;</span></code></pre></div><h2>2. What Is a Reference in C++?</h2><p>A reference is another name, or alias, for an existing variable.</p><p>Unlike a pointer, a reference does not require explicit dereferencing to access the original variable.</p><h3>Example</h3><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code>#<span class="js-id">include</span> <span class="js-op"><</span><span class="js-id">iostream</span><span class="js-op">></span>
<span class="js-id">using</span> <span class="js-id">namespace</span> <span class="js-id">std</span><span class="js-op">;</span>
<span class="js-id">int</span> <span class="js-id">main</span><span class="js-op">()</span> <span class="js-op">{</span>
<span class="js-id">int</span> <span class="js-id">number</span> <span class="js-op">=</span> <span class="js-num">100</span><span class="js-op">;</span>
<span class="js-id">int</span><span class="js-op">&</span> <span class="js-id">ref</span> <span class="js-op">=</span> <span class="js-id">number</span><span class="js-op">;</span>
<span class="js-id">cout</span> <span class="js-op"><<</span> <span class="js-str">"Original value: "</span> <span class="js-op"><<</span> <span class="js-id">number</span> <span class="js-op"><<</span> <span class="js-id">endl</span><span class="js-op">;</span>
<span class="js-id">cout</span> <span class="js-op"><<</span> <span class="js-str">"Reference value: "</span> <span class="js-op"><<</span> <span class="js-id">ref</span> <span class="js-op"><<</span> <span class="js-id">endl</span><span class="js-op">;</span>
<span class="js-kw">return</span> <span class="js-num">0</span><span class="js-op">;</span>
<span class="js-op">}</span></code></pre></div><p><strong>Output:</strong></p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-id">Original</span> <span class="js-id">value</span><span class="js-op">:</span> <span class="js-num">100</span>
<span class="js-id">Reference</span> <span class="js-id">value</span><span class="js-op">:</span> <span class="js-num">100</span></code></pre></div><h3>Explanation</h3><ul><li><p><code>number</code> is the original variable.</p></li><li><p><code>ref</code> is a reference to <code>number</code>.</p></li><li><p>Both names refer to the same integer object.</p></li><li><p>Changing <code>ref</code> also changes <code>number</code>.</p></li></ul><h3>Reference syntax</h3><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-id">data_type</span><span class="js-op">&</span> <span class="js-id">reference_name</span> <span class="js-op">=</span> <span class="js-id">variable_name</span><span class="js-op">;</span></code></pre></div><p>Example:</p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-id">int</span> <span class="js-id">age</span> <span class="js-op">=</span> <span class="js-num">25</span><span class="js-op">;</span>
<span class="js-id">int</span><span class="js-op">&</span> <span class="js-id">ref</span> <span class="js-op">=</span> <span class="js-id">age</span><span class="js-op">;</span></code></pre></div><h2>3. Main Differences Between Pointers and References</h2><p>The most important difference is that a pointer stores an address, while a reference acts as an alias for an existing object.</p><h3>Comparison table</h3><table><tbody><tr><th>Feature</th><th>Pointer</th><th>Reference</th></tr><tr><td>Meaning</td><td>Stores a memory address</td><td>Alias for an existing object</td></tr><tr><td>Declaration</td><td>Uses <code>*</code></td><td>Uses <code>&</code></td></tr><tr><td>Initialization</td><td>Can be initialized later</td><td>Must be initialized when declared</td></tr><tr><td>Null state</td><td>Can be <code>nullptr</code></td><td>No normal null-reference state</td></tr><tr><td>Reassignment</td><td>Can point to another object</td><td>Cannot be reseated</td></tr><tr><td>Access</td><td>Uses <code>*</code> to dereference</td><td>Used like the original variable</td></tr><tr><td>Address</td><td>Stores an address</td><td>Refers to an object's identity</td></tr><tr><td>Arithmetic</td><td>Supported for suitable pointers</td><td>Not supported</td></tr><tr><td>Function parameters</td><td>Can be passed as pointers</td><td>Can be passed as references</td></tr><tr><td>Optional object</td><td>Suitable for representing absence with <code>nullptr</code></td><td>Not suitable for a nullable reference</td></tr><tr><td>Main use</td><td>Address-based operations and optional objects</td><td>Aliases and direct access to existing objects</td></tr></tbody></table><h2>4. Syntax Difference Between Pointer and Reference</h2><p>Pointers and references use different declaration symbols.</p><h3>Pointer declaration</h3><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-id">int</span> <span class="js-id">number</span> <span class="js-op">=</span> <span class="js-num">10</span><span class="js-op">;</span>
<span class="js-id">int</span><span class="js-op">*</span> <span class="js-id">ptr</span> <span class="js-op">=</span> <span class="js-op">&</span><span class="js-id">number</span><span class="js-op">;</span></code></pre></div><p>Here, <code>ptr</code> stores the address of <code>number</code>.</p><p>To access the original value, use:</p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-id">cout</span> <span class="js-op"><<</span> <span class="js-op">*</span><span class="js-id">ptr</span><span class="js-op">;</span></code></pre></div><h3>Reference declaration</h3><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-id">int</span> <span class="js-id">number</span> <span class="js-op">=</span> <span class="js-num">10</span><span class="js-op">;</span>
<span class="js-id">int</span><span class="js-op">&</span> <span class="js-id">ref</span> <span class="js-op">=</span> <span class="js-id">number</span><span class="js-op">;</span></code></pre></div><p>Here, <code>ref</code> acts as another name for <code>number</code>.</p><p>To access the original value, use:</p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-id">cout</span> <span class="js-op"><<</span> <span class="js-id">ref</span><span class="js-op">;</span></code></pre></div><h3>Complete example</h3><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code>#<span class="js-id">include</span> <span class="js-op"><</span><span class="js-id">iostream</span><span class="js-op">></span>
<span class="js-id">using</span> <span class="js-id">namespace</span> <span class="js-id">std</span><span class="js-op">;</span>
<span class="js-id">int</span> <span class="js-id">main</span><span class="js-op">()</span> <span class="js-op">{</span>
<span class="js-id">int</span> <span class="js-id">number</span> <span class="js-op">=</span> <span class="js-num">50</span><span class="js-op">;</span>
<span class="js-id">int</span><span class="js-op">*</span> <span class="js-id">ptr</span> <span class="js-op">=</span> <span class="js-op">&</span><span class="js-id">number</span><span class="js-op">;</span>
<span class="js-id">int</span><span class="js-op">&</span> <span class="js-id">ref</span> <span class="js-op">=</span> <span class="js-id">number</span><span class="js-op">;</span>
<span class="js-id">cout</span> <span class="js-op"><<</span> <span class="js-str">"Using pointer: "</span> <span class="js-op"><<</span> <span class="js-op">*</span><span class="js-id">ptr</span> <span class="js-op"><<</span> <span class="js-id">endl</span><span class="js-op">;</span>
<span class="js-id">cout</span> <span class="js-op"><<</span> <span class="js-str">"Using reference: "</span> <span class="js-op"><<</span> <span class="js-id">ref</span> <span class="js-op"><<</span> <span class="js-id">endl</span><span class="js-op">;</span>
<span class="js-kw">return</span> <span class="js-num">0</span><span class="js-op">;</span>
<span class="js-op">}</span></code></pre></div><p><strong>Output:</strong></p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-id">Using</span> <span class="js-id">pointer</span><span class="js-op">:</span> <span class="js-num">50</span>
<span class="js-id">Using</span> <span class="js-id">reference</span><span class="js-op">:</span> <span class="js-num">50</span></code></pre></div><p>Both provide access to the same original variable.</p><h2>5. Modifying Values Using Pointers and References</h2><p>Both pointers and references can modify the original variable.</p><p>However, their syntax is different.</p><h3>Example: Modifying through a pointer</h3><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code>#<span class="js-id">include</span> <span class="js-op"><</span><span class="js-id">iostream</span><span class="js-op">></span>
<span class="js-id">using</span> <span class="js-id">namespace</span> <span class="js-id">std</span><span class="js-op">;</span>
<span class="js-id">int</span> <span class="js-id">main</span><span class="js-op">()</span> <span class="js-op">{</span>
<span class="js-id">int</span> <span class="js-id">number</span> <span class="js-op">=</span> <span class="js-num">100</span><span class="js-op">;</span>
<span class="js-id">int</span><span class="js-op">*</span> <span class="js-id">ptr</span> <span class="js-op">=</span> <span class="js-op">&</span><span class="js-id">number</span><span class="js-op">;</span>
<span class="js-op">*</span><span class="js-id">ptr</span> <span class="js-op">=</span> <span class="js-num">200</span><span class="js-op">;</span>
<span class="js-id">cout</span> <span class="js-op"><<</span> <span class="js-id">number</span> <span class="js-op"><<</span> <span class="js-id">endl</span><span class="js-op">;</span>
<span class="js-kw">return</span> <span class="js-num">0</span><span class="js-op">;</span>
<span class="js-op">}</span></code></pre></div><p><strong>Output:</strong></p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-num">200</span></code></pre></div><p>The expression <code>*ptr = 200</code> changes the value stored at the address held by <code>ptr</code>.</p><h3>Example: Modifying through a reference</h3><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code>#<span class="js-id">include</span> <span class="js-op"><</span><span class="js-id">iostream</span><span class="js-op">></span>
<span class="js-id">using</span> <span class="js-id">namespace</span> <span class="js-id">std</span><span class="js-op">;</span>
<span class="js-id">int</span> <span class="js-id">main</span><span class="js-op">()</span> <span class="js-op">{</span>
<span class="js-id">int</span> <span class="js-id">number</span> <span class="js-op">=</span> <span class="js-num">100</span><span class="js-op">;</span>
<span class="js-id">int</span><span class="js-op">&</span> <span class="js-id">ref</span> <span class="js-op">=</span> <span class="js-id">number</span><span class="js-op">;</span>
<span class="js-id">ref</span> <span class="js-op">=</span> <span class="js-num">200</span><span class="js-op">;</span>
<span class="js-id">cout</span> <span class="js-op"><<</span> <span class="js-id">number</span> <span class="js-op"><<</span> <span class="js-id">endl</span><span class="js-op">;</span>
<span class="js-kw">return</span> <span class="js-num">0</span><span class="js-op">;</span>
<span class="js-op">}</span></code></pre></div><p><strong>Output:</strong></p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-num">200</span></code></pre></div><p>The expression <code>ref = 200</code> directly modifies the original variable.</p><h2>6. Null Pointer vs Reference</h2><p>One important difference is that pointers can represent the absence of an object, while ordinary valid references must refer to an existing object.</p><h3>Null pointer example</h3><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code>#<span class="js-id">include</span> <span class="js-op"><</span><span class="js-id">iostream</span><span class="js-op">></span>
<span class="js-id">using</span> <span class="js-id">namespace</span> <span class="js-id">std</span><span class="js-op">;</span>
<span class="js-id">int</span> <span class="js-id">main</span><span class="js-op">()</span> <span class="js-op">{</span>
<span class="js-id">int</span><span class="js-op">*</span> <span class="js-id">ptr</span> <span class="js-op">=</span> <span class="js-id">nullptr</span><span class="js-op">;</span>
<span class="js-kw">if</span> <span class="js-op">(</span><span class="js-id">ptr</span> <span class="js-op">==</span> <span class="js-id">nullptr</span><span class="js-op">)</span> <span class="js-op">{</span>
<span class="js-id">cout</span> <span class="js-op"><<</span> <span class="js-str">"Pointer is null"</span> <span class="js-op"><<</span> <span class="js-id">endl</span><span class="js-op">;</span>
<span class="js-op">}</span>
<span class="js-kw">return</span> <span class="js-num">0</span><span class="js-op">;</span>
<span class="js-op">}</span></code></pre></div><p><strong>Output:</strong></p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-id">Pointer</span> <span class="js-id">is</span> <span class="js-num">null</span></code></pre></div><p>A null pointer should never be dereferenced.</p><h3>Reference example</h3><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-id">int</span> <span class="js-id">number</span> <span class="js-op">=</span> <span class="js-num">100</span><span class="js-op">;</span>
<span class="js-id">int</span><span class="js-op">&</span> <span class="js-id">ref</span> <span class="js-op">=</span> <span class="js-id">number</span><span class="js-op">;</span></code></pre></div><p>A reference must be initialized with a valid object.</p><p>You cannot normally declare a reference and leave it empty.</p><p><strong>Important:</strong> C++ does not provide a normal nullable reference type. Avoid creating references that outlive the objects they refer to.</p><h2>7. Reassigning Pointers vs References</h2><p>Pointers can be reassigned to point to different objects. References cannot be reseated after initialization.</p><h3>Example: Reassigning a pointer</h3><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code>#<span class="js-id">include</span> <span class="js-op"><</span><span class="js-id">iostream</span><span class="js-op">></span>
<span class="js-id">using</span> <span class="js-id">namespace</span> <span class="js-id">std</span><span class="js-op">;</span>
<span class="js-id">int</span> <span class="js-id">main</span><span class="js-op">()</span> <span class="js-op">{</span>
<span class="js-id">int</span> <span class="js-id">first</span> <span class="js-op">=</span> <span class="js-num">10</span><span class="js-op">;</span>
<span class="js-id">int</span> <span class="js-id">second</span> <span class="js-op">=</span> <span class="js-num">20</span><span class="js-op">;</span>
<span class="js-id">int</span><span class="js-op">*</span> <span class="js-id">ptr</span> <span class="js-op">=</span> <span class="js-op">&</span><span class="js-id">first</span><span class="js-op">;</span>
<span class="js-id">cout</span> <span class="js-op"><<</span> <span class="js-op">*</span><span class="js-id">ptr</span> <span class="js-op"><<</span> <span class="js-id">endl</span><span class="js-op">;</span>
<span class="js-id">ptr</span> <span class="js-op">=</span> <span class="js-op">&</span><span class="js-id">second</span><span class="js-op">;</span>
<span class="js-id">cout</span> <span class="js-op"><<</span> <span class="js-op">*</span><span class="js-id">ptr</span> <span class="js-op"><<</span> <span class="js-id">endl</span><span class="js-op">;</span>
<span class="js-kw">return</span> <span class="js-num">0</span><span class="js-op">;</span>
<span class="js-op">}</span></code></pre></div><p><strong>Output:</strong></p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-num">10</span>
<span class="js-num">20</span></code></pre></div><p>The pointer initially points to <code>first</code>. Later, it points to <code>second</code>.</p><h3>Example: Assigning through a reference</h3><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code>#<span class="js-id">include</span> <span class="js-op"><</span><span class="js-id">iostream</span><span class="js-op">></span>
<span class="js-id">using</span> <span class="js-id">namespace</span> <span class="js-id">std</span><span class="js-op">;</span>
<span class="js-id">int</span> <span class="js-id">main</span><span class="js-op">()</span> <span class="js-op">{</span>
<span class="js-id">int</span> <span class="js-id">first</span> <span class="js-op">=</span> <span class="js-num">10</span><span class="js-op">;</span>
<span class="js-id">int</span> <span class="js-id">second</span> <span class="js-op">=</span> <span class="js-num">20</span><span class="js-op">;</span>
<span class="js-id">int</span><span class="js-op">&</span> <span class="js-id">ref</span> <span class="js-op">=</span> <span class="js-id">first</span><span class="js-op">;</span>
<span class="js-id">ref</span> <span class="js-op">=</span> <span class="js-id">second</span><span class="js-op">;</span>
<span class="js-id">cout</span> <span class="js-op"><<</span> <span class="js-str">"First: "</span> <span class="js-op"><<</span> <span class="js-id">first</span> <span class="js-op"><<</span> <span class="js-id">endl</span><span class="js-op">;</span>
<span class="js-id">cout</span> <span class="js-op"><<</span> <span class="js-str">"Second: "</span> <span class="js-op"><<</span> <span class="js-id">second</span> <span class="js-op"><<</span> <span class="js-id">endl</span><span class="js-op">;</span>
<span class="js-kw">return</span> <span class="js-num">0</span><span class="js-op">;</span>
<span class="js-op">}</span></code></pre></div><p><strong>Output:</strong></p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-id">First</span><span class="js-op">:</span> <span class="js-num">20</span>
<span class="js-id">Second</span><span class="js-op">:</span> <span class="js-num">20</span></code></pre></div><p>The reference still refers to <code>first</code>. The assignment copies the value of <code>second</code> into <code>first</code>; it does not change what <code>ref</code> refers to.</p><h2>8. Passing Pointers and References to Functions</h2><p>Both pointers and references can be used as function parameters.</p><p>The main difference is how the function receives and accesses the original object.</p><h3>Example: Passing by pointer</h3><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code>#<span class="js-id">include</span> <span class="js-op"><</span><span class="js-id">iostream</span><span class="js-op">></span>
<span class="js-id">using</span> <span class="js-id">namespace</span> <span class="js-id">std</span><span class="js-op">;</span>
<span class="js-kw">void</span> <span class="js-id">updateValue</span><span class="js-op">(</span><span class="js-id">int</span><span class="js-op">*</span> <span class="js-id">ptr</span><span class="js-op">)</span> <span class="js-op">{</span>
<span class="js-kw">if</span> <span class="js-op">(</span><span class="js-id">ptr</span> <span class="js-op">!=</span> <span class="js-id">nullptr</span><span class="js-op">)</span> <span class="js-op">{</span>
<span class="js-op">*</span><span class="js-id">ptr</span> <span class="js-op">=</span> <span class="js-num">500</span><span class="js-op">;</span>
<span class="js-op">}</span>
<span class="js-op">}</span>
<span class="js-id">int</span> <span class="js-id">main</span><span class="js-op">()</span> <span class="js-op">{</span>
<span class="js-id">int</span> <span class="js-id">number</span> <span class="js-op">=</span> <span class="js-num">100</span><span class="js-op">;</span>
<span class="js-id">updateValue</span><span class="js-op">(&</span><span class="js-id">number</span><span class="js-op">);</span>
<span class="js-id">cout</span> <span class="js-op"><<</span> <span class="js-id">number</span> <span class="js-op"><<</span> <span class="js-id">endl</span><span class="js-op">;</span>
<span class="js-kw">return</span> <span class="js-num">0</span><span class="js-op">;</span>
<span class="js-op">}</span></code></pre></div><p><strong>Output:</strong></p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-num">500</span></code></pre></div><p>The function receives the variable's address and modifies the value through the pointer.</p><h3>Example: Passing by reference</h3><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code>#<span class="js-id">include</span> <span class="js-op"><</span><span class="js-id">iostream</span><span class="js-op">></span>
<span class="js-id">using</span> <span class="js-id">namespace</span> <span class="js-id">std</span><span class="js-op">;</span>
<span class="js-kw">void</span> <span class="js-id">updateValue</span><span class="js-op">(</span><span class="js-id">int</span><span class="js-op">&</span> <span class="js-id">value</span><span class="js-op">)</span> <span class="js-op">{</span>
<span class="js-id">value</span> <span class="js-op">=</span> <span class="js-num">500</span><span class="js-op">;</span>
<span class="js-op">}</span>
<span class="js-id">int</span> <span class="js-id">main</span><span class="js-op">()</span> <span class="js-op">{</span>
<span class="js-id">int</span> <span class="js-id">number</span> <span class="js-op">=</span> <span class="js-num">100</span><span class="js-op">;</span>
<span class="js-id">updateValue</span><span class="js-op">(</span><span class="js-id">number</span><span class="js-op">);</span>
<span class="js-id">cout</span> <span class="js-op"><<</span> <span class="js-id">number</span> <span class="js-op"><<</span> <span class="js-id">endl</span><span class="js-op">;</span>
<span class="js-kw">return</span> <span class="js-num">0</span><span class="js-op">;</span>
<span class="js-op">}</span></code></pre></div><p><strong>Output:</strong></p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-num">500</span></code></pre></div><p>The function receives a reference to the original variable and modifies it directly.</p><h3>Comparison</h3><table><tbody><tr><th>Feature</th><th>Pointer parameter</th><th>Reference parameter</th></tr><tr><td>Function call</td><td><code>updateValue(&number)</code></td><td><code>updateValue(number)</code></td></tr><tr><td>Parameter</td><td><code>int* ptr</code></td><td><code>int& value</code></td></tr><tr><td>Access</td><td><code>*ptr</code></td><td><code>value</code></td></tr><tr><td>Can represent absence</td><td>Yes, with <code>nullptr</code></td><td>No normal null state</td></tr><tr><td>Null check</td><td>May be required</td><td>Not normally required</td></tr><tr><td>Typical use</td><td>Optional object or explicit pointer interface</td><td>Required object that may be modified</td></tr></tbody></table><h2>9. Const Pointers vs Const References</h2><p>C++ allows you to use <code>const</code> with both pointers and references.</p><p>However, the meaning depends on where <code>const</code> appears.</p><h3>Pointer to a constant value</h3><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-kw">const</span> <span class="js-id">int</span><span class="js-op">*</span> <span class="js-id">ptr</span><span class="js-op">;</span></code></pre></div><p>You can change the pointer's target, but you cannot modify the pointed-to value through it.</p><h3>Constant pointer</h3><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-id">int</span><span class="js-op">*</span> <span class="js-kw">const</span> <span class="js-id">ptr</span> <span class="js-op">=</span> <span class="js-op">&</span><span class="js-id">number</span><span class="js-op">;</span></code></pre></div><p>You cannot change the pointer's target, but you can modify the pointed-to value through it.</p><h3>Constant pointer to a constant value</h3><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-kw">const</span> <span class="js-id">int</span><span class="js-op">*</span> <span class="js-kw">const</span> <span class="js-id">ptr</span> <span class="js-op">=</span> <span class="js-op">&</span><span class="js-id">number</span><span class="js-op">;</span></code></pre></div><p>You cannot change the pointer's target or modify the value through it.</p><h3>Const reference</h3><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-kw">const</span> <span class="js-id">int</span><span class="js-op">&</span> <span class="js-id">ref</span> <span class="js-op">=</span> <span class="js-id">number</span><span class="js-op">;</span></code></pre></div><p>You cannot modify the original value through this reference.</p><h3>Comparison</h3><table><tbody><tr><th>Declaration</th><th>Change target?</th><th>Modify value through it?</th></tr><tr><td><code>int* ptr</code></td><td>Yes</td><td>Yes</td></tr><tr><td><code>const int* ptr</code></td><td>Yes</td><td>No</td></tr><tr><td><code>int* const ptr</code></td><td>No</td><td>Yes</td></tr><tr><td><code>const int* const ptr</code></td><td>No</td><td>No</td></tr><tr><td><code>const int& ref</code></td><td>Cannot be reseated</td><td>No</td></tr></tbody></table><h2>10. Pointer Arithmetic vs References</h2><p>Pointers support arithmetic operations when they point into an array.</p><p>References do not support pointer arithmetic.</p><h3>Example: Pointer arithmetic</h3><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code>#<span class="js-id">include</span> <span class="js-op"><</span><span class="js-id">iostream</span><span class="js-op">></span>
<span class="js-id">using</span> <span class="js-id">namespace</span> <span class="js-id">std</span><span class="js-op">;</span>
<span class="js-id">int</span> <span class="js-id">main</span><span class="js-op">()</span> <span class="js-op">{</span>
<span class="js-id">int</span> <span class="js-id">numbers</span>[] <span class="js-op">=</span> <span class="js-op">{</span><span class="js-num">10</span><span class="js-op">,</span> <span class="js-num">20</span><span class="js-op">,</span> <span class="js-num">30</span><span class="js-op">,</span> <span class="js-num">40</span><span class="js-op">};</span>
<span class="js-id">int</span><span class="js-op">*</span> <span class="js-id">ptr</span> <span class="js-op">=</span> <span class="js-id">numbers</span><span class="js-op">;</span>
<span class="js-id">cout</span> <span class="js-op"><<</span> <span class="js-op">*</span><span class="js-id">ptr</span> <span class="js-op"><<</span> <span class="js-id">endl</span><span class="js-op">;</span>
<span class="js-id">ptr</span><span class="js-op">++;</span>
<span class="js-id">cout</span> <span class="js-op"><<</span> <span class="js-op">*</span><span class="js-id">ptr</span> <span class="js-op"><<</span> <span class="js-id">endl</span><span class="js-op">;</span>
<span class="js-kw">return</span> <span class="js-num">0</span><span class="js-op">;</span>
<span class="js-op">}</span></code></pre></div><p><strong>Output:</strong></p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-num">10</span>
<span class="js-num">20</span></code></pre></div><p>The pointer moves from the first element to the second element.</p><h3>Reference example</h3><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code>#<span class="js-id">include</span> <span class="js-op"><</span><span class="js-id">iostream</span><span class="js-op">></span>
<span class="js-id">using</span> <span class="js-id">namespace</span> <span class="js-id">std</span><span class="js-op">;</span>
<span class="js-id">int</span> <span class="js-id">main</span><span class="js-op">()</span> <span class="js-op">{</span>
<span class="js-id">int</span> <span class="js-id">numbers</span>[] <span class="js-op">=</span> <span class="js-op">{</span><span class="js-num">10</span><span class="js-op">,</span> <span class="js-num">20</span><span class="js-op">,</span> <span class="js-num">30</span><span class="js-op">};</span>
<span class="js-id">int</span><span class="js-op">&</span> <span class="js-id">ref</span> <span class="js-op">=</span> <span class="js-id">numbers</span>[<span class="js-num">0</span>]<span class="js-op">;</span>
<span class="js-id">cout</span> <span class="js-op"><<</span> <span class="js-id">ref</span> <span class="js-op"><<</span> <span class="js-id">endl</span><span class="js-op">;</span>
<span class="js-id">ref</span> <span class="js-op">=</span> <span class="js-num">100</span><span class="js-op">;</span>
<span class="js-id">cout</span> <span class="js-op"><<</span> <span class="js-id">numbers</span>[<span class="js-num">0</span>] <span class="js-op"><<</span> <span class="js-id">endl</span><span class="js-op">;</span>
<span class="js-kw">return</span> <span class="js-num">0</span><span class="js-op">;</span>
<span class="js-op">}</span></code></pre></div><p><strong>Output:</strong></p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-num">10</span>
<span class="js-num">100</span></code></pre></div><p>The reference provides access to the first array element, but it cannot be incremented to refer to the next element.</p><h2>11. Returning Pointers vs References From Functions</h2><p>Functions can return pointers or references, but their lifetime requirements are important.</p><h3>Returning a pointer</h3><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code>#<span class="js-id">include</span> <span class="js-op"><</span><span class="js-id">iostream</span><span class="js-op">></span>
<span class="js-id">using</span> <span class="js-id">namespace</span> <span class="js-id">std</span><span class="js-op">;</span>
<span class="js-id">int</span><span class="js-op">*</span> <span class="js-id">getValue</span><span class="js-op">(</span><span class="js-id">int</span><span class="js-op">&</span> <span class="js-id">number</span><span class="js-op">)</span> <span class="js-op">{</span>
<span class="js-kw">return</span> <span class="js-op">&</span><span class="js-id">number</span><span class="js-op">;</span>
<span class="js-op">}</span>
<span class="js-id">int</span> <span class="js-id">main</span><span class="js-op">()</span> <span class="js-op">{</span>
<span class="js-id">int</span> <span class="js-id">value</span> <span class="js-op">=</span> <span class="js-num">100</span><span class="js-op">;</span>
<span class="js-id">int</span><span class="js-op">*</span> <span class="js-id">ptr</span> <span class="js-op">=</span> <span class="js-id">getValue</span><span class="js-op">(</span><span class="js-id">value</span><span class="js-op">);</span>
<span class="js-id">cout</span> <span class="js-op"><<</span> <span class="js-op">*</span><span class="js-id">ptr</span> <span class="js-op"><<</span> <span class="js-id">endl</span><span class="js-op">;</span>
<span class="js-kw">return</span> <span class="js-num">0</span><span class="js-op">;</span>
<span class="js-op">}</span></code></pre></div><p><strong>Output:</strong></p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-num">100</span></code></pre></div><p>The function returns the address of an existing object. The object remains alive while <code>main()</code> is using the pointer.</p><h3>Returning a reference</h3><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code>#<span class="js-id">include</span> <span class="js-op"><</span><span class="js-id">iostream</span><span class="js-op">></span>
<span class="js-id">using</span> <span class="js-id">namespace</span> <span class="js-id">std</span><span class="js-op">;</span>
<span class="js-id">int</span><span class="js-op">&</span> <span class="js-id">getValue</span><span class="js-op">(</span><span class="js-id">int</span><span class="js-op">&</span> <span class="js-id">number</span><span class="js-op">)</span> <span class="js-op">{</span>
<span class="js-kw">return</span> <span class="js-id">number</span><span class="js-op">;</span>
<span class="js-op">}</span>
<span class="js-id">int</span> <span class="js-id">main</span><span class="js-op">()</span> <span class="js-op">{</span>
<span class="js-id">int</span> <span class="js-id">value</span> <span class="js-op">=</span> <span class="js-num">100</span><span class="js-op">;</span>
<span class="js-id">getValue</span><span class="js-op">(</span><span class="js-id">value</span><span class="js-op">)</span> <span class="js-op">=</span> <span class="js-num">500</span><span class="js-op">;</span>
<span class="js-id">cout</span> <span class="js-op"><<</span> <span class="js-id">value</span> <span class="js-op"><<</span> <span class="js-id">endl</span><span class="js-op">;</span>
<span class="js-kw">return</span> <span class="js-num">0</span><span class="js-op">;</span>
<span class="js-op">}</span></code></pre></div><p><strong>Output:</strong></p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-num">500</span></code></pre></div><p>The function returns a reference to the original object.</p><h3>Important lifetime rule</h3><p>Never return a pointer or reference to a local variable that is destroyed when the function returns.</p><p>Both pointers and references can become dangling when the object they refer to no longer exists.</p><h2>12. Pointers and References With Classes</h2><p>Pointers and references are also useful when working with C++ classes and objects.</p><h3>Example: Using a pointer with an object</h3><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code>#<span class="js-id">include</span> <span class="js-op"><</span><span class="js-id">iostream</span><span class="js-op">></span>
<span class="js-id">using</span> <span class="js-id">namespace</span> <span class="js-id">std</span><span class="js-op">;</span>
<span class="js-kw">class</span> <span class="js-id">Student</span> <span class="js-op">{</span>
<span class="js-id">public</span><span class="js-op">:</span>
<span class="js-kw">void</span> <span class="js-id">display</span><span class="js-op">()</span> <span class="js-op">{</span>
<span class="js-id">cout</span> <span class="js-op"><<</span> <span class="js-str">"Student information"</span> <span class="js-op"><<</span> <span class="js-id">endl</span><span class="js-op">;</span>
<span class="js-op">}</span>
<span class="js-op">};</span>
<span class="js-id">int</span> <span class="js-id">main</span><span class="js-op">()</span> <span class="js-op">{</span>
<span class="js-id">Student</span> <span class="js-id">student</span><span class="js-op">;</span>
<span class="js-id">Student</span><span class="js-op">*</span> <span class="js-id">ptr</span> <span class="js-op">=</span> <span class="js-op">&</span><span class="js-id">student</span><span class="js-op">;</span>
<span class="js-id">ptr</span><span class="js-op">-></span><span class="js-id">display</span><span class="js-op">();</span>
<span class="js-kw">return</span> <span class="js-num">0</span><span class="js-op">;</span>
<span class="js-op">}</span></code></pre></div><p><strong>Output:</strong></p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-id">Student</span> <span class="js-id">information</span></code></pre></div><p>The arrow operator (<code>-></code>) accesses a member through a pointer.</p><h3>Example: Using a reference with an object</h3><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code>#<span class="js-id">include</span> <span class="js-op"><</span><span class="js-id">iostream</span><span class="js-op">></span>
<span class="js-id">using</span> <span class="js-id">namespace</span> <span class="js-id">std</span><span class="js-op">;</span>
<span class="js-kw">class</span> <span class="js-id">Student</span> <span class="js-op">{</span>
<span class="js-id">public</span><span class="js-op">:</span>
<span class="js-kw">void</span> <span class="js-id">display</span><span class="js-op">()</span> <span class="js-op">{</span>
<span class="js-id">cout</span> <span class="js-op"><<</span> <span class="js-str">"Student information"</span> <span class="js-op"><<</span> <span class="js-id">endl</span><span class="js-op">;</span>
<span class="js-op">}</span>
<span class="js-op">};</span>
<span class="js-id">int</span> <span class="js-id">main</span><span class="js-op">()</span> <span class="js-op">{</span>
<span class="js-id">Student</span> <span class="js-id">student</span><span class="js-op">;</span>
<span class="js-id">Student</span><span class="js-op">&</span> <span class="js-id">ref</span> <span class="js-op">=</span> <span class="js-id">student</span><span class="js-op">;</span>
<span class="js-id">ref</span><span class="js-op">.</span><span class="js-id">display</span><span class="js-op">();</span>
<span class="js-kw">return</span> <span class="js-num">0</span><span class="js-op">;</span>
<span class="js-op">}</span></code></pre></div><p><strong>Output:</strong></p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-id">Student</span> <span class="js-id">information</span></code></pre></div><p>A reference is used like the original object, so the dot operator (<code>.</code>) accesses its members.</p><h2>13. Practical Use Cases of Pointers</h2><p>Pointers are useful when a program needs explicit control over addresses or optional access to objects.</p><p>Common applications include:</p><ol start="1"><li><p>Dynamic memory allocation.</p></li><li><p>Linked lists and tree structures.</p></li><li><p>Pointer arithmetic and array traversal.</p></li><li><p>Function callbacks.</p></li><li><p>Working with APIs that use pointer parameters.</p></li><li><p>Representing optional objects with <code>nullptr</code>.</p></li><li><p>Polymorphism through base-class pointers.</p></li><li><p>Low-level and system programming.</p></li></ol><h3>Example: Pointer with dynamic memory</h3><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code>#<span class="js-id">include</span> <span class="js-op"><</span><span class="js-id">iostream</span><span class="js-op">></span>
<span class="js-id">using</span> <span class="js-id">namespace</span> <span class="js-id">std</span><span class="js-op">;</span>
<span class="js-id">int</span> <span class="js-id">main</span><span class="js-op">()</span> <span class="js-op">{</span>
<span class="js-id">int</span><span class="js-op">*</span> <span class="js-id">ptr</span> <span class="js-op">=</span> <span class="js-kw">new</span> <span class="js-id">int</span><span class="js-op">(</span><span class="js-num">100</span><span class="js-op">);</span>
<span class="js-id">cout</span> <span class="js-op"><<</span> <span class="js-op">*</span><span class="js-id">ptr</span> <span class="js-op"><<</span> <span class="js-id">endl</span><span class="js-op">;</span>
<span class="js-id">delete</span> <span class="js-id">ptr</span><span class="js-op">;</span>
<span class="js-id">ptr</span> <span class="js-op">=</span> <span class="js-id">nullptr</span><span class="js-op">;</span>
<span class="js-kw">return</span> <span class="js-num">0</span><span class="js-op">;</span>
<span class="js-op">}</span></code></pre></div><p><strong>Output:</strong></p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-num">100</span></code></pre></div><p>In modern C++, prefer smart pointers such as <code>std::unique_ptr</code> for managing dynamically allocated objects.</p><h2>14. Practical Use Cases of References</h2><p>References are useful when you need a direct alias for an existing object.</p><p>Common applications include:</p><ol start="1"><li><p>Passing arguments to functions without copying.</p></li><li><p>Modifying original variables through function parameters.</p></li><li><p>Returning existing objects from functions.</p></li><li><p>Accessing class objects through aliases.</p></li><li><p>Using const references for read-only access.</p></li><li><p>Supporting operator overloading.</p></li><li><p>Implementing move semantics with rvalue references.</p></li></ol><h3>Example: Swapping two values using references</h3><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code>#<span class="js-id">include</span> <span class="js-op"><</span><span class="js-id">iostream</span><span class="js-op">></span>
<span class="js-id">using</span> <span class="js-id">namespace</span> <span class="js-id">std</span><span class="js-op">;</span>
<span class="js-kw">void</span> <span class="js-id">swapValues</span><span class="js-op">(</span><span class="js-id">int</span><span class="js-op">&</span> <span class="js-id">a</span><span class="js-op">,</span> <span class="js-id">int</span><span class="js-op">&</span> <span class="js-id">b</span><span class="js-op">)</span> <span class="js-op">{</span>
<span class="js-id">int</span> <span class="js-id">temp</span> <span class="js-op">=</span> <span class="js-id">a</span><span class="js-op">;</span>
<span class="js-id">a</span> <span class="js-op">=</span> <span class="js-id">b</span><span class="js-op">;</span>
<span class="js-id">b</span> <span class="js-op">=</span> <span class="js-id">temp</span><span class="js-op">;</span>
<span class="js-op">}</span>
<span class="js-id">int</span> <span class="js-id">main</span><span class="js-op">()</span> <span class="js-op">{</span>
<span class="js-id">int</span> <span class="js-id">first</span> <span class="js-op">=</span> <span class="js-num">10</span><span class="js-op">;</span>
<span class="js-id">int</span> <span class="js-id">second</span> <span class="js-op">=</span> <span class="js-num">20</span><span class="js-op">;</span>
<span class="js-id">swapValues</span><span class="js-op">(</span><span class="js-id">first</span><span class="js-op">,</span> <span class="js-id">second</span><span class="js-op">);</span>
<span class="js-id">cout</span> <span class="js-op"><<</span> <span class="js-id">first</span> <span class="js-op"><<</span> <span class="js-id">endl</span><span class="js-op">;</span>
<span class="js-id">cout</span> <span class="js-op"><<</span> <span class="js-id">second</span> <span class="js-op"><<</span> <span class="js-id">endl</span><span class="js-op">;</span>
<span class="js-kw">return</span> <span class="js-num">0</span><span class="js-op">;</span>
<span class="js-op">}</span></code></pre></div><p><strong>Output:</strong></p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-num">20</span>
<span class="js-num">10</span></code></pre></div><p>The function modifies the original variables without returning a separate result.</p><h2>15. When Should You Use a Pointer or a Reference?</h2><p>Choosing between a pointer and a reference depends on the requirements of your program.</p><h3>Use a pointer when:</h3><ul><li><p>The object may be absent, represented by <code>nullptr</code>.</p></li><li><p>You need to change which object is being referenced.</p></li><li><p>You need pointer arithmetic.</p></li><li><p>You are working with an API that expects a pointer.</p></li><li><p>You need to express explicit address-based behavior.</p></li></ul><h3>Use a reference when:</h3><ul><li><p>An object is required and must already exist.</p></li><li><p>You want to modify an existing variable through a function.</p></li><li><p>You need read-only access through a const reference.</p></li><li><p>You want a simpler alternative to pointer-based function parameters.</p></li><li><p>You need an alias for an existing object.</p></li></ul><h3>Quick decision table</h3><table><tbody><tr><th>Requirement</th><th>Recommended choice</th></tr><tr><td>Required object parameter</td><td>Reference</td></tr><tr><td>Optional object parameter</td><td>Pointer</td></tr><tr><td>Modify an existing variable</td><td>Non-const reference</td></tr><tr><td>Read a large object without copying</td><td>Const reference</td></tr><tr><td>Reassign the target</td><td>Pointer</td></tr><tr><td>Traverse array elements through addresses</td><td>Pointer</td></tr><tr><td>Access an object through an alias</td><td>Reference</td></tr><tr><td>Represent no object</td><td><code>nullptr</code> pointer</td></tr></tbody></table><h2>16. Common Mistakes When Using Pointers and References</h2><h3>1. Dereferencing a null pointer</h3><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-id">int</span><span class="js-op">*</span> <span class="js-id">ptr</span> <span class="js-op">=</span> <span class="js-id">nullptr</span><span class="js-op">;</span>
<span class="js-cm">// Invalid: *ptr = 100;</span></code></pre></div><p><strong>Solution:</strong> Check that a pointer is valid before dereferencing it.</p><h3>2. Using an uninitialized pointer</h3><p>An uninitialized pointer contains an indeterminate value.</p><p><strong>Solution:</strong> Initialize pointers with a valid address or <code>nullptr</code>.</p><h3>3. Declaring a reference without initialization</h3><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-id">int</span><span class="js-op">&</span> <span class="js-id">ref</span><span class="js-op">;</span> <span class="js-cm">// Compilation error</span></code></pre></div><p><strong>Solution:</strong> Initialize the reference with a valid object.</p><h3>4. Trying to reassign a reference</h3><p>Assigning another variable to a reference changes the original referred-to object; it does not reseat the reference.</p><p><strong>Solution:</strong> Use a pointer when the target needs to change.</p><h3>5. Returning a reference to a local variable</h3><p>The local variable is destroyed when the function ends.</p><p><strong>Solution:</strong> Return references only to objects with a suitable lifetime.</p><h3>6. Confusing pointer and reference syntax</h3><p>Pointers require dereferencing with <code>*</code>, while references are accessed like ordinary variables.</p><p><strong>Solution:</strong> Practise both forms with small examples.</p><h3>7. Assuming references guarantee object lifetime</h3><p>A reference can become dangling if the object it refers to is destroyed.</p><p><strong>Solution:</strong> Ensure the referenced object outlives every use of the reference.</p><h2>17. Best Practices for Pointers and References</h2><p>Follow these practices when working with pointers and references:</p><ol start="1"><li><p>Initialize pointers before using them.</p></li><li><p>Use <code>nullptr</code> to represent an absent object.</p></li><li><p>Initialize references when declaring them.</p></li><li><p>Prefer references for required function arguments.</p></li><li><p>Use const references for read-only access to larger objects.</p></li><li><p>Use pointers when nullability or reseating is needed.</p></li><li><p>Avoid returning references or pointers to local variables.</p></li><li><p>Keep object lifetimes and ownership clear.</p></li><li><p>Prefer smart pointers for dynamic ownership management.</p></li><li><p>Choose the simplest interface that clearly expresses your program's requirements.</p></li></ol><h2>18. Frequently Asked Questions (FAQs)</h2><h3>Q1. What is the main difference between pointers and references in C++?</h3><p>A pointer stores a memory address, while a reference acts as another name for an existing object.</p><h3>Q2. Which is better, a pointer or a reference?</h3><p>Neither is universally better. References are usually simpler for required objects, while pointers are useful for optional objects, reseating, and address-based operations.</p><h3>Q3. Can a reference be null in C++?</h3><p>A valid ordinary C++ reference must refer to an object or function. It does not have a normal nullable state like a pointer.</p><h3>Q4. Can pointers be reassigned?</h3><p>Yes. A pointer can be changed to store the address of another compatible object.</p><h3>Q5. Can references be reassigned?</h3><p>No. A reference cannot be reseated after initialization. Assigning another value through it modifies the original object.</p><h3>Q6. Can pointers and references modify original variables?</h3><p>Yes. A non-const pointer or reference can modify the object it refers to.</p><h3>Q7. Can a function return a pointer or reference?</h3><p>Yes. However, the returned pointer or reference must not refer to an object whose lifetime has ended.</p><h3>Q8. Are references faster than pointers?</h3><p>There is no general performance rule that references are always faster. Compilers optimize both, and performance depends on the program and generated code.</p><h3>Q9. Can references be used with arrays?</h3><p>Yes. References can refer to individual array elements or entire arrays. C++ does not allow ordinary arrays of references.</p><h3>Q10. What is the difference between a const pointer and a const reference?</h3><p>A const pointer restricts whether the pointer itself or its pointed-to value can change, depending on its declaration. A const reference prevents modification through that reference.</p>
Code Example
#include <iostream>
struct Profile {
int id;
std::string name;
};
void updateViaPointer(Profile* p) {
if (p) p->name = "Updated by Pointer";
}
void updateViaReference(Profile& p) {
p.name = "Updated by Reference"; // Clean syntax, guaranteed non-null
}
int main() {
Profile user{101, "Original"};
updateViaPointer(&user);
std::cout << "Pointer update: " << user.name << "
";
updateViaReference(user);
std::cout << "Reference update: " << user.name << "
";
return 0;
}Interview Question & Tips
A: Prefer references whenever an argument is mandatory (non-optional/non-nullable) and operator syntax should be natural. Use pointers when an argument is optional (nullable via `nullptr`) or when implementing dynamic data structures (linked lists, trees) that require node rebinding.
Q: Does a reference occupy memory in assembly?
A: In machine code, compilers typically implement references as `const` pointers under the hood, but the optimizer frequently eliminates storage altogether by substituting direct register usage.
Best Practices
2. Use pointers (`std::unique_ptr` or `nullptr`-capable raw pointers) when "absence of value" is a valid state.
3. Avoid returning pointers or references to local stack-allocated objects.
This study reference for Pointers vs References in C++: Behavior, Syntax, and Use Cases is curated and verified against recognized curriculum benchmarks, peer-reviewed textbooks, and official technical documentation: