C++ Programming 5. C++ Pointers and References 15 mins read 21,805 views

Pointers With Arrays in C++: Traversing Elements Through Addresses

Pointers and arrays are closely connected in C++. A pointer can store the address of an array element and use that address to access or traverse elements in the...
Published: Nov 1, 2026Last Updated: Nov 21, 2026Curriculum: Class 12 / AP Physics / JEE-NEETReviewed by: OperateTools Editorial Board
Quick Summary & Direct Answer (AEO / GEO Extraction)

What is Pointers With Arrays in C++: Traversing Elements Through Addresses? Pointers and arrays are closely connected in C++. A pointer can store the address of an array element and use that address to access or traverse elements in the...

Audience & Scope: Students, educators, and candidates preparing for academic examinations and technical assessments in C++ Programming.
How does pointer arithmetic navigate contiguous array elements in memory?
<p>Pointers and arrays are closely connected in C++. A pointer can store the address of an array element and use that address to access or traverse elements in the array.</p><p><br></p><p>Instead of accessing array elements only through indexes, you can use pointers to move between elements and work directly with their memory addresses.</p><p><br></p><p>This concept is useful when working with arrays, functions, dynamic memory, strings, and data structures.</p>

Explanation & In-Depth Details

<h2 class="h4 fw-bold text-dark mb-3"><span style="font-size: 1.15rem;">1. What Are Pointers With Arrays in C++?</span></h2><p>An array stores multiple elements of the same data type in a sequence of memory locations.</p><p>A pointer can store the address of an array element. In many expressions, an array name automatically converts into a pointer to its first element.</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">&lt;</span><span class="js-id">iostream</span><span class="js-op">&gt;</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-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-id">numbers</span><span class="js-op">;</span>

<span class="js-id">cout</span> <span class="js-op">&lt;&lt;</span> <span class="js-str">"First element: "</span> <span class="js-op">&lt;&lt;</span> <span class="js-op">*</span><span class="js-id">ptr</span> <span class="js-op">&lt;&lt;</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-id">element</span><span class="js-op">:</span> <span class="js-num">10</span></code></pre></div><h3>Explanation</h3><ul><li><p><code>numbers</code> is an integer array.</p></li><li><p><code>ptr</code> is an integer pointer.</p></li><li><p><code>numbers</code> converts to the address of its first element in this assignment.</p></li><li><p><code>*ptr</code> accesses the first array element.</p></li></ul><p>The following two statements are equivalent:</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-op">*</span> <span class="js-id">ptr</span> <span class="js-op">=</span> <span class="js-id">numbers</span><span class="js-op">;</span></code></pre></div><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-op">&amp;</span><span class="js-id">numbers</span>[<span class="js-num">0</span>]<span class="js-op">;</span></code></pre></div><p>Both initialize <code>ptr</code> with the address of the first element.</p><h2>2. Understanding Array Memory Addresses</h2><p>Array elements are stored in contiguous memory locations.</p><p>This means the elements are arranged one after another, with no gaps between consecutive elements of the array.</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">&lt;</span><span class="js-id">iostream</span><span class="js-op">&gt;</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-kw">for</span> <span class="js-op">(</span><span class="js-id">int</span> <span class="js-id">i</span> <span class="js-op">=</span> <span class="js-num">0</span><span class="js-op">;</span> <span class="js-id">i</span> <span class="js-op">&lt;</span> <span class="js-num">4</span><span class="js-op">;</span> <span class="js-id">i</span><span class="js-op">++)</span> <span class="js-op">{</span>
<span class="js-id">cout</span> <span class="js-op">&lt;&lt;</span> <span class="js-str">"Value: "</span> <span class="js-op">&lt;&lt;</span> <span class="js-id">numbers</span>[<span class="js-id">i</span>]
<span class="js-op">&lt;&lt;</span> <span class="js-str">" Address: "</span> <span class="js-op">&lt;&lt;</span> <span class="js-op">&amp;</span><span class="js-id">numbers</span>[<span class="js-id">i</span>] <span class="js-op">&lt;&lt;</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>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">Value</span><span class="js-op">:</span> <span class="js-num">10</span> <span class="js-id">Address</span><span class="js-op">:</span> 0x1000
<span class="js-id">Value</span><span class="js-op">:</span> <span class="js-num">20</span> <span class="js-id">Address</span><span class="js-op">:</span> 0x1004
<span class="js-id">Value</span><span class="js-op">:</span> <span class="js-num">30</span> <span class="js-id">Address</span><span class="js-op">:</span> 0x1008
<span class="js-id">Value</span><span class="js-op">:</span> <span class="js-num">40</span> <span class="js-id">Address</span><span class="js-op">:</span> 0x100c</code></pre></div><p><em>The addresses are illustrative. Actual addresses depend on the system and program execution.</em></p><p>If an integer occupies 4 bytes on the system, consecutive elements will typically have addresses separated by 4 bytes.</p><p>The important point is that array elements are stored in contiguous memory.</p><h2>3. Accessing Array Elements Using Pointers</h2><p>You can use a pointer to access array elements without directly writing an array index.</p><h3>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-op">*(</span><span class="js-id">pointer</span> <span class="js-op">+</span> <span class="js-id">index</span><span class="js-op">)</span></code></pre></div><p>Here:</p><ul><li><p><code>pointer</code> stores the address of the first array element.</p></li><li><p><code>index</code> represents the number of elements to move forward.</p></li><li><p><code>*</code> accesses the value at the resulting address.</p></li></ul><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">&lt;</span><span class="js-id">iostream</span><span class="js-op">&gt;</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-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-id">numbers</span><span class="js-op">;</span>

<span class="js-id">cout</span> <span class="js-op">&lt;&lt;</span> <span class="js-op">*(</span><span class="js-id">ptr</span> <span class="js-op">+</span> <span class="js-num">0</span><span class="js-op">)</span> <span class="js-op">&lt;&lt;</span> <span class="js-id">endl</span><span class="js-op">;</span>
<span class="js-id">cout</span> <span class="js-op">&lt;&lt;</span> <span class="js-op">*(</span><span class="js-id">ptr</span> <span class="js-op">+</span> <span class="js-num">1</span><span class="js-op">)</span> <span class="js-op">&lt;&lt;</span> <span class="js-id">endl</span><span class="js-op">;</span>
<span class="js-id">cout</span> <span class="js-op">&lt;&lt;</span> <span class="js-op">*(</span><span class="js-id">ptr</span> <span class="js-op">+</span> <span class="js-num">2</span><span class="js-op">)</span> <span class="js-op">&lt;&lt;</span> <span class="js-id">endl</span><span class="js-op">;</span>
<span class="js-id">cout</span> <span class="js-op">&lt;&lt;</span> <span class="js-op">*(</span><span class="js-id">ptr</span> <span class="js-op">+</span> <span class="js-num">3</span><span class="js-op">)</span> <span class="js-op">&lt;&lt;</span> <span class="js-id">endl</span><span class="js-op">;</span>
<span class="js-id">cout</span> <span class="js-op">&lt;&lt;</span> <span class="js-op">*(</span><span class="js-id">ptr</span> <span class="js-op">+</span> <span class="js-num">4</span><span class="js-op">)</span> <span class="js-op">&lt;&lt;</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>
<span class="js-num">30</span>
<span class="js-num">40</span>
<span class="js-num">50</span></code></pre></div><h3>Explanation</h3><table><tbody><tr><th>Expression</th><th>Array element</th><th>Value</th></tr><tr><td><code>*(ptr + 0)</code></td><td><code>numbers[0]</code></td><td>10</td></tr><tr><td><code>*(ptr + 1)</code></td><td><code>numbers[1]</code></td><td>20</td></tr><tr><td><code>*(ptr + 2)</code></td><td><code>numbers[2]</code></td><td>30</td></tr><tr><td><code>*(ptr + 3)</code></td><td><code>numbers[3]</code></td><td>40</td></tr><tr><td><code>*(ptr + 4)</code></td><td><code>numbers[4]</code></td><td>50</td></tr></tbody></table><p>This is called pointer-based array access.</p><h2>4. Traversing an Array Using Pointer Arithmetic</h2><p>Pointer arithmetic allows you to move between array elements by incrementing or decrementing a pointer.</p><p>When you increment a pointer, it moves to the next element of its pointed-to type.</p><h3>Example: Using the increment operator</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">&lt;</span><span class="js-id">iostream</span><span class="js-op">&gt;</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-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-id">numbers</span><span class="js-op">;</span>

<span class="js-kw">for</span> <span class="js-op">(</span><span class="js-id">int</span> <span class="js-id">i</span> <span class="js-op">=</span> <span class="js-num">0</span><span class="js-op">;</span> <span class="js-id">i</span> <span class="js-op">&lt;</span> <span class="js-num">5</span><span class="js-op">;</span> <span class="js-id">i</span><span class="js-op">++)</span> <span class="js-op">{</span>
<span class="js-id">cout</span> <span class="js-op">&lt;&lt;</span> <span class="js-op">*</span><span class="js-id">ptr</span> <span class="js-op">&lt;&lt;</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-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>
<span class="js-num">30</span>
<span class="js-num">40</span>
<span class="js-num">50</span></code></pre></div><h3>How does it work?</h3><ol start="1"><li><p>The pointer initially points to the first element.</p></li><li><p><code>*ptr</code> reads the current element.</p></li><li><p><code>ptr++</code> moves the pointer to the next element.</p></li><li><p>The loop repeats until all five elements have been processed.</p></li></ol><h3>Important pointer arithmetic operations</h3><table><tbody><tr><th>Operation</th><th>Description</th></tr><tr><td><code>ptr++</code></td><td>Moves to the next element</td></tr><tr><td><code>ptr--</code></td><td>Moves to the previous element</td></tr><tr><td><code>ptr + 2</code></td><td>Points two elements forward</td></tr><tr><td><code>ptr - 2</code></td><td>Points two elements backward</td></tr><tr><td><code>*ptr</code></td><td>Accesses the current element</td></tr><tr><td><code>ptr[i]</code></td><td>Accesses the element at offset <code>i</code></td></tr></tbody></table><p>Pointer arithmetic must stay within the same array object or its one-past position. The one-past pointer must never be dereferenced.</p><h2>5. Difference Between Array Indexing and Pointer Access</h2><p>C++ allows you to access array elements using either array indexing or pointer expressions.</p><p>Both approaches can access the same element.</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">&lt;</span><span class="js-id">iostream</span><span class="js-op">&gt;</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">5</span><span class="js-op">,</span> <span class="js-num">10</span><span class="js-op">,</span> <span class="js-num">15</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-id">numbers</span><span class="js-op">;</span>

<span class="js-id">cout</span> <span class="js-op">&lt;&lt;</span> <span class="js-id">numbers</span>[<span class="js-num">2</span>] <span class="js-op">&lt;&lt;</span> <span class="js-id">endl</span><span class="js-op">;</span>
<span class="js-id">cout</span> <span class="js-op">&lt;&lt;</span> <span class="js-op">*(</span><span class="js-id">ptr</span> <span class="js-op">+</span> <span class="js-num">2</span><span class="js-op">)</span> <span class="js-op">&lt;&lt;</span> <span class="js-id">endl</span><span class="js-op">;</span>
<span class="js-id">cout</span> <span class="js-op">&lt;&lt;</span> <span class="js-id">ptr</span>[<span class="js-num">2</span>] <span class="js-op">&lt;&lt;</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">15</span>
<span class="js-num">15</span>
<span class="js-num">15</span></code></pre></div><h3>Comparison</h3><table><tbody><tr><th>Array indexing</th><th>Pointer access</th></tr><tr><td><code>numbers[0]</code></td><td><code>*(ptr + 0)</code></td></tr><tr><td><code>numbers[1]</code></td><td><code>*(ptr + 1)</code></td></tr><tr><td><code>numbers[2]</code></td><td><code>*(ptr + 2)</code></td></tr><tr><td><code>numbers[3]</code></td><td><code>*(ptr + 3)</code></td></tr></tbody></table><p>The expression <code>ptr[i]</code> is also equivalent to <code>*(ptr + i)</code>.</p><p><strong>Remember:</strong> Array indexing is often easier to read. Pointer access is useful when learning memory operations and when working with pointer-based interfaces.</p><h2>6. Printing All Array Elements Using a Pointer</h2><p>You can traverse an array using a pointer and a loop without directly using array indexing.</p><h3>Example: Using a while loop</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">&lt;</span><span class="js-id">iostream</span><span class="js-op">&gt;</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">12</span><span class="js-op">,</span> <span class="js-num">24</span><span class="js-op">,</span> <span class="js-num">36</span><span class="js-op">,</span> <span class="js-num">48</span><span class="js-op">,</span> <span class="js-num">60</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">int</span><span class="js-op">*</span> <span class="js-id">end</span> <span class="js-op">=</span> <span class="js-id">numbers</span> <span class="js-op">+</span> <span class="js-num">5</span><span class="js-op">;</span>

<span class="js-kw">while</span> <span class="js-op">(</span><span class="js-id">ptr</span> <span class="js-op">&lt;</span> <span class="js-id">end</span><span class="js-op">)</span> <span class="js-op">{</span>
<span class="js-id">cout</span> <span class="js-op">&lt;&lt;</span> <span class="js-op">*</span><span class="js-id">ptr</span> <span class="js-op">&lt;&lt;</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-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">12</span>
<span class="js-num">24</span>
<span class="js-num">36</span>
<span class="js-num">48</span>
<span class="js-num">60</span></code></pre></div><h3>Explanation</h3><ul><li><p><code>ptr</code> points to the first element.</p></li><li><p><code>end</code> points one position past the final element.</p></li><li><p>The loop continues while <code>ptr</code> is before <code>end</code>.</p></li><li><p><code>*ptr</code> prints the current element.</p></li><li><p><code>ptr++</code> advances to the next element.</p></li></ul><p>Using a one-past pointer as the loop boundary is a common C++ traversal technique. Do not dereference <code>end</code>.</p><h2>7. Modifying Array Elements Using Pointers</h2><p>Pointers can also modify array elements.</p><p>When you assign a new value through a pointer, the corresponding array element changes.</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">&lt;</span><span class="js-id">iostream</span><span class="js-op">&gt;</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-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-id">numbers</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">0</span><span class="js-op">)</span> <span class="js-op">=</span> <span class="js-num">100</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">1</span><span class="js-op">)</span> <span class="js-op">=</span> <span class="js-num">200</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">2</span><span class="js-op">)</span> <span class="js-op">=</span> <span class="js-num">300</span><span class="js-op">;</span>

<span class="js-kw">for</span> <span class="js-op">(</span><span class="js-id">int</span> <span class="js-id">i</span> <span class="js-op">=</span> <span class="js-num">0</span><span class="js-op">;</span> <span class="js-id">i</span> <span class="js-op">&lt;</span> <span class="js-num">5</span><span class="js-op">;</span> <span class="js-id">i</span><span class="js-op">++)</span> <span class="js-op">{</span>
<span class="js-id">cout</span> <span class="js-op">&lt;&lt;</span> <span class="js-id">numbers</span>[<span class="js-id">i</span>] <span class="js-op">&lt;&lt;</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-num">100</span>
<span class="js-num">200</span>
<span class="js-num">300</span>
<span class="js-num">40</span>
<span class="js-num">50</span></code></pre></div><h3>Explanation</h3><ul><li><p><code>*(ptr + 0) = 100</code> updates the first element.</p></li><li><p><code>*(ptr + 1) = 200</code> updates the second element.</p></li><li><p><code>*(ptr + 2) = 300</code> updates the third element.</p></li></ul><p>The pointer provides access to the original array elements, so the changes are visible through the array name.</p><h2>8. Passing an Array to a Function Using a Pointer</h2><p>When an array is passed to a function as a parameter, the parameter is adjusted to a pointer type.</p><p>The function can use that pointer to access the original array elements.</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">&lt;</span><span class="js-id">iostream</span><span class="js-op">&gt;</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">display</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">int</span> <span class="js-id">size</span><span class="js-op">)</span> <span class="js-op">{</span>
<span class="js-kw">for</span> <span class="js-op">(</span><span class="js-id">int</span> <span class="js-id">i</span> <span class="js-op">=</span> <span class="js-num">0</span><span class="js-op">;</span> <span class="js-id">i</span> <span class="js-op">&lt;</span> <span class="js-id">size</span><span class="js-op">;</span> <span class="js-id">i</span><span class="js-op">++)</span> <span class="js-op">{</span>
<span class="js-id">cout</span> <span class="js-op">&lt;&lt;</span> <span class="js-op">*(</span><span class="js-id">ptr</span> <span class="js-op">+</span> <span class="js-id">i</span><span class="js-op">)</span> <span class="js-op">&lt;&lt;</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">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-num">50</span><span class="js-op">};</span>

<span class="js-id">display</span><span class="js-op">(</span><span class="js-id">numbers</span><span class="js-op">,</span> <span class="js-num">5</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>
<span class="js-num">30</span>
<span class="js-num">40</span>
<span class="js-num">50</span></code></pre></div><h3>Explanation</h3><ul><li><p><code>display()</code> receives an integer pointer and an array size.</p></li><li><p><code>numbers</code> converts to a pointer to its first element when passed.</p></li><li><p>The function accesses each element using pointer arithmetic.</p></li><li><p>The <code>size</code> parameter tells the function how many elements it can safely process.</p></li></ul><p><strong>Important:</strong> An array parameter does not automatically carry the array's length. Pass the size separately, or use a suitable standard library container or array reference when preserving size information is important.</p><h2>9. Finding the Sum of Array Elements Using Pointers</h2><p>Pointers can be used to process array elements and calculate their total.</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">&lt;</span><span class="js-id">iostream</span><span class="js-op">&gt;</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-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-id">numbers</span><span class="js-op">;</span>
<span class="js-id">int</span> <span class="js-id">sum</span> <span class="js-op">=</span> <span class="js-num">0</span><span class="js-op">;</span>

<span class="js-kw">for</span> <span class="js-op">(</span><span class="js-id">int</span> <span class="js-id">i</span> <span class="js-op">=</span> <span class="js-num">0</span><span class="js-op">;</span> <span class="js-id">i</span> <span class="js-op">&lt;</span> <span class="js-num">5</span><span class="js-op">;</span> <span class="js-id">i</span><span class="js-op">++)</span> <span class="js-op">{</span>
<span class="js-id">sum</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">i</span><span class="js-op">);</span>
<span class="js-op">}</span>

<span class="js-id">cout</span> <span class="js-op">&lt;&lt;</span> <span class="js-str">"Sum: "</span> <span class="js-op">&lt;&lt;</span> <span class="js-id">sum</span> <span class="js-op">&lt;&lt;</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">Sum</span><span class="js-op">:</span> <span class="js-num">150</span></code></pre></div><h3>Explanation</h3><p>The loop accesses each element through the pointer and adds it to <code>sum</code>.</p><p>This approach can be used for calculating totals, averages, minimum values, and maximum values.</p><h2>10. Finding the Largest Element Using Pointers</h2><p>You can compare array elements through a pointer to find the largest value.</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">&lt;</span><span class="js-id">iostream</span><span class="js-op">&gt;</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">25</span><span class="js-op">,</span> <span class="js-num">80</span><span class="js-op">,</span> <span class="js-num">15</span><span class="js-op">,</span> <span class="js-num">100</span><span class="js-op">,</span> <span class="js-num">45</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">int</span> <span class="js-id">largest</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-kw">for</span> <span class="js-op">(</span><span class="js-id">int</span> <span class="js-id">i</span> <span class="js-op">=</span> <span class="js-num">1</span><span class="js-op">;</span> <span class="js-id">i</span> <span class="js-op">&lt;</span> <span class="js-num">5</span><span class="js-op">;</span> <span class="js-id">i</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">i</span><span class="js-op">)</span> <span class="js-op">&gt;</span> <span class="js-id">largest</span><span class="js-op">)</span> <span class="js-op">{</span>
<span class="js-id">largest</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">i</span><span class="js-op">);</span>
<span class="js-op">}</span>
<span class="js-op">}</span>

<span class="js-id">cout</span> <span class="js-op">&lt;&lt;</span> <span class="js-str">"Largest value: "</span> <span class="js-op">&lt;&lt;</span> <span class="js-id">largest</span> <span class="js-op">&lt;&lt;</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">Largest</span> <span class="js-id">value</span><span class="js-op">:</span> <span class="js-num">100</span></code></pre></div><p>The program starts with the first element as the largest value. It compares the remaining elements and updates the result whenever it finds a larger number.</p><h2>11. Reversing an Array Using Pointers</h2><p>Pointers can also traverse an array from both ends.</p><p>One pointer starts at the first element, and another starts at the last element. Their values are swapped until the pointers meet or cross.</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">&lt;</span><span class="js-id">iostream</span><span class="js-op">&gt;</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-num">50</span><span class="js-op">};</span>

<span class="js-id">int</span><span class="js-op">*</span> <span class="js-id">left</span> <span class="js-op">=</span> <span class="js-id">numbers</span><span class="js-op">;</span>
<span class="js-id">int</span><span class="js-op">*</span> <span class="js-id">right</span> <span class="js-op">=</span> <span class="js-id">numbers</span> <span class="js-op">+</span> <span class="js-num">4</span><span class="js-op">;</span>

<span class="js-kw">while</span> <span class="js-op">(</span><span class="js-id">left</span> <span class="js-op">&lt;</span> <span class="js-id">right</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-op">*</span><span class="js-id">left</span><span class="js-op">;</span>
<span class="js-op">*</span><span class="js-id">left</span> <span class="js-op">=</span> <span class="js-op">*</span><span class="js-id">right</span><span class="js-op">;</span>
<span class="js-op">*</span><span class="js-id">right</span> <span class="js-op">=</span> <span class="js-id">temp</span><span class="js-op">;</span>

<span class="js-id">left</span><span class="js-op">++;</span>
<span class="js-id">right</span><span class="js-op">--;</span>
<span class="js-op">}</span>

<span class="js-kw">for</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-id">numbers</span><span class="js-op">)</span> <span class="js-op">{</span>
<span class="js-id">cout</span> <span class="js-op">&lt;&lt;</span> <span class="js-id">value</span> <span class="js-op">&lt;&lt;</span> <span class="js-str">" "</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-num">50</span> <span class="js-num">40</span> <span class="js-num">30</span> <span class="js-num">20</span> <span class="js-num">10</span></code></pre></div><p>This technique is useful for understanding two-pointer algorithms.</p><p>For arrays of different sizes, calculate the final element using the array's actual size rather than hard-coding the index.</p><h2>12. Pointers With Two-Dimensional Arrays</h2><p>C++ also supports pointers with multidimensional arrays.</p><p>A two-dimensional array is stored as an array of rows, with each row containing its own sequence of elements.</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">&lt;</span><span class="js-id">iostream</span><span class="js-op">&gt;</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-num">2</span>][<span class="js-num">3</span>] <span class="js-op">=</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-op">{</span><span class="js-num">40</span><span class="js-op">,</span> <span class="js-num">50</span><span class="js-op">,</span> <span class="js-num">60</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-num">3</span>] <span class="js-op">=</span> <span class="js-id">numbers</span><span class="js-op">;</span>

<span class="js-kw">for</span> <span class="js-op">(</span><span class="js-id">int</span> <span class="js-id">i</span> <span class="js-op">=</span> <span class="js-num">0</span><span class="js-op">;</span> <span class="js-id">i</span> <span class="js-op">&lt;</span> <span class="js-num">2</span><span class="js-op">;</span> <span class="js-id">i</span><span class="js-op">++)</span> <span class="js-op">{</span>
<span class="js-kw">for</span> <span class="js-op">(</span><span class="js-id">int</span> <span class="js-id">j</span> <span class="js-op">=</span> <span class="js-num">0</span><span class="js-op">;</span> <span class="js-id">j</span> <span class="js-op">&lt;</span> <span class="js-num">3</span><span class="js-op">;</span> <span class="js-id">j</span><span class="js-op">++)</span> <span class="js-op">{</span>
<span class="js-id">cout</span> <span class="js-op">&lt;&lt;</span> <span class="js-id">ptr</span>[<span class="js-id">i</span>][<span class="js-id">j</span>] <span class="js-op">&lt;&lt;</span> <span class="js-str">" "</span><span class="js-op">;</span>
<span class="js-op">}</span>

<span class="js-id">cout</span> <span class="js-op">&lt;&lt;</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-num">10</span> <span class="js-num">20</span> <span class="js-num">30</span>
<span class="js-num">40</span> <span class="js-num">50</span> <span class="js-num">60</span></code></pre></div><h3>Explanation</h3><ul><li><p><code>numbers</code> is a two-dimensional array with two rows and three columns.</p></li><li><p><code>int (*ptr)[3]</code> declares a pointer to an array of three integers.</p></li><li><p><code>ptr[i][j]</code> accesses the element in row <code>i</code> and column <code>j</code>.</p></li></ul><p>The parentheses are important. <code>int (*ptr)[3]</code> is a pointer to an array, while <code>int* ptr[3]</code> declares an array of three integer pointers.</p><h2>13. Pointer to an Array vs Array of Pointers</h2><p>These two declarations may look similar, but they have different meanings.</p><h3>Pointer to an array</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">numbers</span>[<span class="js-num">3</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">ptr</span><span class="js-op">)</span>[<span class="js-num">3</span>] <span class="js-op">=</span> <span class="js-op">&amp;</span><span class="js-id">numbers</span><span class="js-op">;</span></code></pre></div><p>Here, <code>ptr</code> points to the entire array of three integers.</p><h3>Array of pointers</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">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-id">third</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">ptr</span>[<span class="js-num">3</span>] <span class="js-op">=</span> <span class="js-op">{&amp;</span><span class="js-id">first</span><span class="js-op">,</span> <span class="js-op">&amp;</span><span class="js-id">second</span><span class="js-op">,</span> <span class="js-op">&amp;</span><span class="js-id">third</span><span class="js-op">};</span></code></pre></div><p>Here, <code>ptr</code> is an array containing three integer pointers.</p><h3>Comparison</h3><table><tbody><tr><th>Feature</th><th>Pointer to an array</th><th>Array of pointers</th></tr><tr><td>Declaration</td><td><code>int (*ptr)[3]</code></td><td><code>int* ptr[3]</code></td></tr><tr><td>Meaning</td><td>Points to an entire array</td><td>Stores multiple pointers</td></tr><tr><td>Common use</td><td>Multidimensional arrays</td><td>Referencing separate variables or objects</td></tr><tr><td>Structure</td><td>One pointer</td><td>Array of pointer elements</td></tr></tbody></table><h2>14. Difference Between Array Names and Pointers</h2><p>An array name often converts to a pointer to its first element, but an array and a pointer are not the same thing.</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">&lt;</span><span class="js-id">iostream</span><span class="js-op">&gt;</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">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">&lt;&lt;</span> <span class="js-str">"Array first element: "</span> <span class="js-op">&lt;&lt;</span> <span class="js-op">*</span><span class="js-id">numbers</span> <span class="js-op">&lt;&lt;</span> <span class="js-id">endl</span><span class="js-op">;</span>
<span class="js-id">cout</span> <span class="js-op">&lt;&lt;</span> <span class="js-str">"Pointer first element: "</span> <span class="js-op">&lt;&lt;</span> <span class="js-op">*</span><span class="js-id">ptr</span> <span class="js-op">&lt;&lt;</span> <span class="js-id">endl</span><span class="js-op">;</span>

<span class="js-id">cout</span> <span class="js-op">&lt;&lt;</span> <span class="js-str">"Array size: "</span> <span class="js-op">&lt;&lt;</span> <span class="js-id">sizeof</span><span class="js-op">(</span><span class="js-id">numbers</span><span class="js-op">)</span> <span class="js-op">&lt;&lt;</span> <span class="js-id">endl</span><span class="js-op">;</span>
<span class="js-id">cout</span> <span class="js-op">&lt;&lt;</span> <span class="js-str">"Pointer size: "</span> <span class="js-op">&lt;&lt;</span> <span class="js-id">sizeof</span><span class="js-op">(</span><span class="js-id">ptr</span><span class="js-op">)</span> <span class="js-op">&lt;&lt;</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 on a system with 4-byte integers and 8-byte pointers:</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">Array</span> <span class="js-id">first</span> <span class="js-id">element</span><span class="js-op">:</span> <span class="js-num">10</span>
<span class="js-id">Pointer</span> <span class="js-id">first</span> <span class="js-id">element</span><span class="js-op">:</span> <span class="js-num">10</span>
<span class="js-id">Array</span> <span class="js-id">size</span><span class="js-op">:</span> <span class="js-num">12</span>
<span class="js-id">Pointer</span> <span class="js-id">size</span><span class="js-op">:</span> <span class="js-num">8</span></code></pre></div><p>The sizes shown are system-dependent.</p><h3>Important differences</h3><table><tbody><tr><th>Array</th><th>Pointer</th></tr><tr><td>Stores its elements</td><td>Stores an address</td></tr><tr><td>Has a fixed number of elements</td><td>Can point to different objects of a compatible type</td></tr><tr><td><code>sizeof(array)</code> gives total array storage size</td><td><code>sizeof(pointer)</code> gives pointer size</td></tr><tr><td>Cannot be reassigned as a whole</td><td>Can be reassigned</td></tr><tr><td>Array name converts to a pointer in many expressions</td><td>Pointer is already an address-holding variable</td></tr></tbody></table><h2>15. Common Mistakes When Using Pointers With Arrays</h2><p>Beginners often make these mistakes when working with pointers and arrays.</p><h3>1. Accessing elements outside the array</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">numbers</span>[<span class="js-num">3</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">ptr</span> <span class="js-op">=</span> <span class="js-id">numbers</span><span class="js-op">;</span>

<span class="js-cm">// Invalid: ptr[3] is outside the array.</span></code></pre></div><p><strong>Solution:</strong> Access only valid indexes, from <code>0</code> to <code>size - 1</code>.</p><h3>2. Dereferencing an uninitialized 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-cm">// Unsafe: the pointer is uninitialized.</span></code></pre></div><p><strong>Solution:</strong> Initialize the pointer with a valid address or <code>nullptr</code>.</p><h3>3. Forgetting pointer increment</h3><p>If you print <code>*ptr</code> repeatedly without moving the pointer or changing the index, you will keep reading the same element.</p><p><strong>Solution:</strong> Use <code>ptr++</code> or <code>*(ptr + i)</code> when traversing the array.</p><h3>4. Confusing a pointer with an array</h3><p>An array name and a pointer are related, but they are different types.</p><p><strong>Solution:</strong> Understand array-to-pointer conversion and the difference between <code>sizeof(array)</code> and <code>sizeof(pointer)</code>.</p><h3>5. Using an invalid pointer after the array's lifetime ends</h3><p>A pointer becomes invalid when the object it refers to no longer exists.</p><p><strong>Solution:</strong> Make sure the array remains alive whenever you access it through a pointer.</p><h3>6. Incorrect 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-op">*</span> <span class="js-id">first</span><span class="js-op">,</span> <span class="js-id">second</span><span class="js-op">;</span></code></pre></div><p>Only <code>first</code> is a pointer. <code>second</code> is an integer.</p><p><strong>Solution:</strong> Declare each pointer clearly:</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-op">*</span> <span class="js-id">first</span><span class="js-op">;</span>
<span class="js-id">int</span><span class="js-op">*</span> <span class="js-id">second</span><span class="js-op">;</span></code></pre></div><h2>16. Best Practices for Pointers With Arrays</h2><p>Follow these practices when using pointers with arrays:</p><ol start="1"><li><p>Initialize pointers before accessing array elements.</p></li><li><p>Always know the array's size.</p></li><li><p>Avoid accessing elements outside the array boundaries.</p></li><li><p>Use <code>const int*</code> when a function should not modify array elements.</p></li><li><p>Prefer array indexing when it makes the code easier to understand.</p></li><li><p>Use pointer arithmetic when it makes the intended operation clearer.</p></li><li><p>Avoid returning pointers to local arrays.</p></li><li><p>Use standard library containers such as <code>std::array</code> and <code>std::vector</code> when appropriate.</p></li><li><p>Keep pointer and array lifetimes clear.</p></li><li><p>Compile and test examples with warnings enabled.</p></li></ol><h2>17. Frequently Asked Questions (FAQs)</h2><h3>Q1. How are pointers related to arrays in C++?</h3><p>In many expressions, an array name converts to a pointer to its first element. A pointer can then be used to access and traverse the array elements.</p><h3>Q2. How do you declare a pointer for an array?</h3><p>For an integer array, you can declare and initialize a pointer like this:</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">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">ptr</span> <span class="js-op">=</span> <span class="js-id">numbers</span><span class="js-op">;</span></code></pre></div><h3>Q3. What does *(ptr + i) mean?</h3><p>It accesses the array element located <code>i</code> positions after the element pointed to by <code>ptr</code>.</p><h3>Q4. Can we traverse an array using a pointer?</h3><p>Yes. You can use pointer increment (<code>ptr++</code>) or pointer arithmetic (<code>*(ptr + i)</code>) to access array elements one by one.</p><h3>Q5. Can pointers modify array elements?</h3><p>Yes. If the pointer points to a modifiable array element, you can change its value using an expression such as <code>*ptr = 100</code>.</p><h3>Q6. What is the difference between numbers[i] and *(numbers + i)?</h3><p>Both expressions access the same array element. C++ defines array subscripting in terms of pointer arithmetic and dereferencing.</p><h3>Q7. Can we use pointers with two-dimensional arrays?</h3><p>Yes. A pointer to an array can be used to access rows and columns of a two-dimensional array.</p><h3>Q8. What is an array of pointers?</h3><p>An array of pointers is an array whose elements are pointers. For example, <code>int* ptr[3]</code> declares an array containing three integer pointers.</p><h3>Q9. Are arrays and pointers identical in C++?</h3><p>No. Arrays and pointers are different types. Array-to-pointer conversion makes them behave similarly in many expressions, but they have different storage and size properties.</p><h3>Q10. Why are pointers useful for array traversal?</h3><p>Pointers allow direct access to array elements through their addresses. They are useful for understanding memory, working with low-level interfaces, and implementing pointer-based algorithms.</p>

Code Example

#include <iostream>

int main() {
    int buffer[5] = {10, 20, 30, 40, 50};
    int* ptr = buffer; // Decays to &buffer[0]

    std::cout << "Traversing via Pointer Arithmetic:
";
    for (int i = 0; i < 5; ++i) {
        std::cout << "Element [" << i << "]: " << *(ptr + i) 
                  << " at address: " << (ptr + i) << "
";
    }

    // Pointer difference (number of elements between pointers)
    int* endPtr = buffer + 5;
    std::cout << "Total elements (end - start): " << (endPtr - ptr) << "
";
    return 0;
}

Interview Question & Tips

Q: What happens when you subtract two pointers in C++?
A: Pointer subtraction (`ptr2 - ptr1`) returns a signed integer of type `std::ptrdiff_t` representing the number of array elements between the two memory addresses, not the raw byte difference.

Q: Why is pointer decay a potential source of bugs?
A: When an array is passed to a function, it decays to a pointer, losing its compile-time size information (i.e. `sizeof(arr)` returns pointer size 8 bytes rather than total array bytes).

Best Practices

1. Use `std::array` or `std::vector` instead of C-style raw arrays to preserve size information.
2. Use `std::span` (C++20) for non-owning views over contiguous memory buffers.
3. Never increment pointers beyond the past-the-end element (`arr + N`).
Academic Standards & Verified Citations

This study reference for Pointers With Arrays in C++: Traversing Elements Through Addresses is curated and verified against recognized curriculum benchmarks, peer-reviewed textbooks, and official technical documentation:

C++ Programming