C++ Programming 7. C++ Structures and Unions 14 mins read 18,805 views

C++ Unions: Shared Memory Storage and Practical Examples

C++ provides several user-defined data types that help organize information. Structures (struct) group multiple data members together, while unions (union) allo...
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 C++ Unions: Shared Memory Storage and Practical Examples? C++ provides several user-defined data types that help organize information. Structures (struct) group multiple data members together, while unions (union) allo...

Audience & Scope: Students, educators, and candidates preparing for academic examinations and technical assessments in C++ Programming.
How do unions share a single memory location across multiple members and how does std::variant improve type safety?
<p>C++ provides several user-defined data types that help organize information. Structures (<code>struct</code>) group multiple data members together, while unions (<code>union</code>) allow multiple members to share the same memory location.</p><p>Unions are useful when a program needs to store one value from several possible data types at a time.</p><p>For example, an application might need to store:</p><ul><li><p>An integer value.</p></li><li><p>A floating-point value.</p></li><li><p>A character.</p></li><li><p>A Boolean value.</p></li></ul><p>Instead of allocating separate storage for every member, a union provides shared storage for its members.</p>

Explanation & In-Depth Details

<h2 class="h4 fw-bold text-dark mb-3"><span style="font-size: 1.15rem;">1. What Is a Union in C++?</span></h2><p>A union is a user-defined data type in C++ in which all non-static data members share the same memory location.</p><p>Unlike a structure, where each member has its own storage, union members overlap in memory.</p><p>This means that a union is generally used to hold one active member at a time.</p><h3>Syntax of a Union</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">union</span> <span class="js-id">UnionName</span> <span class="js-op">{</span>
<span class="js-id">data_type</span> <span class="js-id">member1</span><span class="js-op">;</span>
<span class="js-id">data_type</span> <span class="js-id">member2</span><span class="js-op">;</span>
<span class="js-id">data_type</span> <span class="js-id">member3</span><span class="js-op">;</span>
<span class="js-op">};</span></code></pre></div><p>Here:</p><ul><li><p><code>union</code> is the keyword used to define a union.</p></li><li><p><code>UnionName</code> is the name of the union.</p></li><li><p><code>member1</code>, <code>member2</code>, and <code>member3</code> are its data members.</p></li><li><p>The semicolon after the closing brace is required.</p></li></ul><h3>Example: Declare a Union</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">union</span> <span class="js-id">Data</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-id">float</span> <span class="js-id">decimal</span><span class="js-op">;</span>
<span class="js-id">char</span> <span class="js-id">letter</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-kw">return</span> <span class="js-num">0</span><span class="js-op">;</span>
<span class="js-op">}</span></code></pre></div><p>The <code>Data</code> union contains three members:</p><table><tbody><tr><th>Member</th><th>Data type</th><th>Purpose</th></tr><tr><td><code>number</code></td><td><code>int</code></td><td>Stores an integer</td></tr><tr><td><code>decimal</code></td><td><code>float</code></td><td>Stores a decimal value</td></tr><tr><td><code>letter</code></td><td><code>char</code></td><td>Stores a character</td></tr></tbody></table><p>All three members share the same storage.</p><h2>2. How Does a Union Work in C++?</h2><p>The main feature of a union is shared memory.</p><p>Consider this union:</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">union</span> <span class="js-id">Data</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-id">float</span> <span class="js-id">decimal</span><span class="js-op">;</span>
<span class="js-id">char</span> <span class="js-id">letter</span><span class="js-op">;</span>
<span class="js-op">};</span></code></pre></div><p>The compiler allocates enough storage to accommodate its largest member, subject to the type's alignment requirements.</p><p>For example, if an implementation uses:</p><ul><li><p><code>int</code> requiring 4 bytes,</p></li><li><p><code>float</code> requiring 4 bytes,</p></li><li><p><code>char</code> requiring 1 byte,</p></li></ul><p>the union will typically require 4 bytes, although its exact size depends on the implementation and alignment.</p><p>&lt;AsyncImage query="C++ union shared memory educational diagram overlapping int float char members same memory block labeled union memory layout simple programming infographic" aspectRatio="16:9" width="100%" maxHeight={220}/&gt;</p><p><strong>Important:</strong> The members do not have separate independent values. Writing to one member changes the shared storage and generally ends the lifetime of the previously active member.</p><h2>3. Declaring and Creating Union Variables</h2><p>After defining a union, you can create variables using its name.</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-id">UnionName</span> <span class="js-id">variableName</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">union</span> <span class="js-id">Data</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-id">float</span> <span class="js-id">decimal</span><span class="js-op">;</span>
<span class="js-id">char</span> <span class="js-id">letter</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">Data</span> <span class="js-id">value</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>Here, <code>value</code> is a union object.</p><p>You can also create multiple union variables:</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">Data</span> <span class="js-id">first</span><span class="js-op">;</span>
<span class="js-id">Data</span> <span class="js-id">second</span><span class="js-op">;</span>
<span class="js-id">Data</span> <span class="js-id">third</span><span class="js-op">;</span></code></pre></div><p>Each union object has its own shared storage.</p><h2>4. Accessing Union Members</h2><p>The dot (<code>.</code>) operator is used to access union members through an object.</p><h3>Example: Store and Display an Integer</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">union</span> <span class="js-id">Data</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-id">float</span> <span class="js-id">decimal</span><span class="js-op">;</span>
<span class="js-id">char</span> <span class="js-id">letter</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">Data</span> <span class="js-id">value</span><span class="js-op">;</span>

<span class="js-id">value</span><span class="js-op">.</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">cout</span> <span class="js-op">&lt;&lt;</span> <span class="js-str">"Number: "</span> <span class="js-op">&lt;&lt;</span> <span class="js-id">value</span><span class="js-op">.</span><span class="js-id">number</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>Output:</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">Number</span><span class="js-op">:</span> <span class="js-num">100</span></code></pre></div><p>The program stores an integer in the union and reads that same active member.</p><h2>5. Understanding Shared Memory With an Example</h2><p>When you assign a value to one union member, that member becomes active.</p><p>Consider this 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">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">union</span> <span class="js-id">Data</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-id">float</span> <span class="js-id">decimal</span><span class="js-op">;</span>
<span class="js-id">char</span> <span class="js-id">letter</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">Data</span> <span class="js-id">value</span><span class="js-op">;</span>

<span class="js-id">value</span><span class="js-op">.</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">cout</span> <span class="js-op">&lt;&lt;</span> <span class="js-str">"Integer: "</span> <span class="js-op">&lt;&lt;</span> <span class="js-id">value</span><span class="js-op">.</span><span class="js-id">number</span> <span class="js-op">&lt;&lt;</span> <span class="js-id">endl</span><span class="js-op">;</span>

<span class="js-id">value</span><span class="js-op">.</span><span class="js-id">decimal</span> <span class="js-op">=</span> <span class="js-num">25</span><span class="js-op">.</span>5f<span class="js-op">;</span>

<span class="js-id">cout</span> <span class="js-op">&lt;&lt;</span> <span class="js-str">"Float: "</span> <span class="js-op">&lt;&lt;</span> <span class="js-id">value</span><span class="js-op">.</span><span class="js-id">decimal</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>Output:</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">Integer</span><span class="js-op">:</span> <span class="js-num">100</span>
<span class="js-id">Float</span><span class="js-op">:</span> <span class="js-num">25.5</span></code></pre></div><p>Explanation:</p><ol start="1"><li><p><code>value.number = 100</code> activates the integer member.</p></li><li><p>The program reads and displays the integer.</p></li><li><p><code>value.decimal = 25.5f</code> activates the floating-point member.</p></li><li><p>The previous integer member is no longer the active member.</p></li><li><p>The program reads and displays the floating-point value.</p></li></ol><p>The example does not read the inactive integer member after assigning the float.</p><p><strong>Important:</strong> Do not assume that assigning one union member allows you to read another member as though it still contains its original value. Reading an inactive member is generally not permitted, apart from specific language rules such as inspecting the common initial sequence of standard-layout struct members in an appropriate union.</p><h2>6. Initializing a Union in C++</h2><p>You can initialize a union when declaring its variable.</p><h3>Example 1: Initialize the First Member</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">union</span> <span class="js-id">Data</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-id">float</span> <span class="js-id">decimal</span><span class="js-op">;</span>
<span class="js-id">char</span> <span class="js-id">letter</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">Data</span> <span class="js-id">value</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-id">cout</span> <span class="js-op">&lt;&lt;</span> <span class="js-id">value</span><span class="js-op">.</span><span class="js-id">number</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>Output:</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>Traditional aggregate initialization without a designated member initializes the first member.</p><h3>Example 2: Initialize a Specific Member Using a Designated Initializer</h3><p>C++20 supports designated initializers for aggregates.</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">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">union</span> <span class="js-id">Data</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-id">float</span> <span class="js-id">decimal</span><span class="js-op">;</span>
<span class="js-id">char</span> <span class="js-id">letter</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">Data</span> <span class="js-id">value</span> <span class="js-op">=</span> <span class="js-op">{.</span><span class="js-id">decimal</span> <span class="js-op">=</span> <span class="js-num">25</span><span class="js-op">.</span>5f<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">.</span><span class="js-id">decimal</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>Output:</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">25.5</span></code></pre></div><p>The designated initializer identifies which member should be initialized.</p><h2>7. Updating Union Members</h2><p>You can change the active member by assigning a value to another member.</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">union</span> <span class="js-id">Data</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-id">float</span> <span class="js-id">decimal</span><span class="js-op">;</span>
<span class="js-id">char</span> <span class="js-id">letter</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">Data</span> <span class="js-id">value</span><span class="js-op">;</span>

<span class="js-id">value</span><span class="js-op">.</span><span class="js-id">letter</span> <span class="js-op">=</span> <span class="js-str">'A'</span><span class="js-op">;</span>
<span class="js-id">cout</span> <span class="js-op">&lt;&lt;</span> <span class="js-str">"Character: "</span> <span class="js-op">&lt;&lt;</span> <span class="js-id">value</span><span class="js-op">.</span><span class="js-id">letter</span> <span class="js-op">&lt;&lt;</span> <span class="js-id">endl</span><span class="js-op">;</span>

<span class="js-id">value</span><span class="js-op">.</span><span class="js-id">number</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">&lt;&lt;</span> <span class="js-str">"Integer: "</span> <span class="js-op">&lt;&lt;</span> <span class="js-id">value</span><span class="js-op">.</span><span class="js-id">number</span> <span class="js-op">&lt;&lt;</span> <span class="js-id">endl</span><span class="js-op">;</span>

<span class="js-id">value</span><span class="js-op">.</span><span class="js-id">decimal</span> <span class="js-op">=</span> <span class="js-num">12</span><span class="js-op">.</span>5f<span class="js-op">;</span>
<span class="js-id">cout</span> <span class="js-op">&lt;&lt;</span> <span class="js-str">"Float: "</span> <span class="js-op">&lt;&lt;</span> <span class="js-id">value</span><span class="js-op">.</span><span class="js-id">decimal</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>Output:</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">Character</span><span class="js-op">:</span> <span class="js-id">A</span>
<span class="js-id">Integer</span><span class="js-op">:</span> <span class="js-num">500</span>
<span class="js-id">Float</span><span class="js-op">:</span> <span class="js-num">12.5</span></code></pre></div><p>Each assignment activates a different member. Only the currently active member is read.</p><h2>8. Finding the Size of a Union</h2><p>The <code>sizeof</code> operator returns the size of a union object in bytes.</p><p>The size is sufficient to hold its largest member, taking alignment and implementation requirements into account.</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">union</span> <span class="js-id">Data</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-id">double</span> <span class="js-id">decimal</span><span class="js-op">;</span>
<span class="js-id">char</span> <span class="js-id">letter</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">cout</span> <span class="js-op">&lt;&lt;</span> <span class="js-str">"Size of int: "</span> <span class="js-op">&lt;&lt;</span> <span class="js-id">sizeof</span><span class="js-op">(</span><span class="js-id">int</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">"Size of double: "</span> <span class="js-op">&lt;&lt;</span> <span class="js-id">sizeof</span><span class="js-op">(</span><span class="js-id">double</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">"Size of char: "</span> <span class="js-op">&lt;&lt;</span> <span class="js-id">sizeof</span><span class="js-op">(</span><span class="js-id">char</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">"Size of union: "</span> <span class="js-op">&lt;&lt;</span> <span class="js-id">sizeof</span><span class="js-op">(</span><span class="js-id">Data</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>Example output on a common platform:</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">Size</span> <span class="js-kw">of</span> <span class="js-id">int</span><span class="js-op">:</span> <span class="js-num">4</span>
<span class="js-id">Size</span> <span class="js-kw">of</span> <span class="js-id">double</span><span class="js-op">:</span> <span class="js-num">8</span>
<span class="js-id">Size</span> <span class="js-kw">of</span> <span class="js-id">char</span><span class="js-op">:</span> <span class="js-num">1</span>
<span class="js-id">Size</span> <span class="js-kw">of</span> <span class="js-id">union</span><span class="js-op">:</span> <span class="js-num">8</span></code></pre></div><p>The exact output depends on the platform and compiler.</p><p>The union size is not necessarily the simple sum of its member sizes. It is generally at least as large as its largest member and must also satisfy alignment requirements.</p><h2>9. Array of Unions in C++</h2><p>You can create an array of union objects just as you can create an array of structures.</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">union</span> <span class="js-id">Data</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-id">float</span> <span class="js-id">decimal</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">Data</span> <span class="js-id">values</span>[<span class="js-num">3</span>]<span class="js-op">;</span>

<span class="js-id">values</span>[<span class="js-num">0</span>]<span class="js-op">.</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">values</span>[<span class="js-num">1</span>]<span class="js-op">.</span><span class="js-id">decimal</span> <span class="js-op">=</span> <span class="js-num">20</span><span class="js-op">.</span>5f<span class="js-op">;</span>
<span class="js-id">values</span>[<span class="js-num">2</span>]<span class="js-op">.</span><span class="js-id">number</span> <span class="js-op">=</span> <span class="js-num">30</span><span class="js-op">;</span>

<span class="js-id">cout</span> <span class="js-op">&lt;&lt;</span> <span class="js-id">values</span>[<span class="js-num">0</span>]<span class="js-op">.</span><span class="js-id">number</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">values</span>[<span class="js-num">1</span>]<span class="js-op">.</span><span class="js-id">decimal</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">values</span>[<span class="js-num">2</span>]<span class="js-op">.</span><span class="js-id">number</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>Output:</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.5</span>
<span class="js-num">30</span></code></pre></div><p>Each element in the array is a separate union object with its own shared storage.</p><h2>10. Pointers to Unions</h2><p>You can create a pointer that stores the address of a union object.</p><p>Use the arrow (<code>-&gt;</code>) operator to access members through the pointer.</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">union</span> <span class="js-id">Data</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-id">float</span> <span class="js-id">decimal</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">Data</span> <span class="js-id">value</span><span class="js-op">;</span>

<span class="js-id">Data</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">value</span><span class="js-op">;</span>

<span class="js-id">ptr</span><span class="js-op">-&gt;</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">cout</span> <span class="js-op">&lt;&lt;</span> <span class="js-str">"Number: "</span> <span class="js-op">&lt;&lt;</span> <span class="js-id">ptr</span><span class="js-op">-&gt;</span><span class="js-id">number</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>Output:</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">Number</span><span class="js-op">:</span> <span class="js-num">100</span></code></pre></div><p>Here:</p><ul><li><p><code>ptr</code> stores the address of the union object.</p></li><li><p><code>ptr-&gt;number</code> accesses the integer member.</p></li><li><p>The integer member becomes active when assigned.</p></li></ul><p>The arrow operator is equivalent to dereferencing the pointer and using the dot operator:</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-op">(*</span><span class="js-id">ptr</span><span class="js-op">).</span><span class="js-id">number</span><span class="js-op">;</span></code></pre></div><h2>11. Passing a Union to a Function</h2><p>Like structures, unions can be passed to functions by value, by reference, or by constant reference.</p><h3>Example: Pass a Union by 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-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">union</span> <span class="js-id">Data</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-id">float</span> <span class="js-id">decimal</span><span class="js-op">;</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">Data</span> <span class="js-id">value</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">"Number: "</span> <span class="js-op">&lt;&lt;</span> <span class="js-id">value</span><span class="js-op">.</span><span class="js-id">number</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-id">int</span> <span class="js-id">main</span><span class="js-op">()</span> <span class="js-op">{</span>
<span class="js-id">Data</span> <span class="js-id">data</span><span class="js-op">;</span>

<span class="js-id">data</span><span class="js-op">.</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">display</span><span class="js-op">(</span><span class="js-id">data</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>Output:</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">Number</span><span class="js-op">:</span> <span class="js-num">100</span></code></pre></div><p>The function receives a copy of the union object. In this example, the integer member is active in the copied object.</p><h3>Example: Pass a Union 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">&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">union</span> <span class="js-id">Data</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-id">float</span> <span class="js-id">decimal</span><span class="js-op">;</span>
<span class="js-op">};</span>

<span class="js-kw">void</span> <span class="js-id">update</span><span class="js-op">(</span><span class="js-id">Data</span><span class="js-op">&amp;</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-id">number</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">Data</span> <span class="js-id">data</span><span class="js-op">;</span>

<span class="js-id">data</span><span class="js-op">.</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">update</span><span class="js-op">(</span><span class="js-id">data</span><span class="js-op">);</span>

<span class="js-id">cout</span> <span class="js-op">&lt;&lt;</span> <span class="js-id">data</span><span class="js-op">.</span><span class="js-id">number</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>Output:</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>Passing by reference allows the function to update the original union object.</p><h2>12. Union With a Structure in C++</h2><p>A union can be used as a member of a structure. This is useful when a record can contain one of several possible value types.</p><p>However, the program should also store information identifying which union member is active.</p><p>This is commonly called a tagged union or discriminated union.</p><h3>Example: Structure With a Union Member</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">struct</span> <span class="js-id">Value</span> <span class="js-op">{</span>
<span class="js-id">int</span> <span class="js-id">type</span><span class="js-op">;</span>

<span class="js-id">union</span> <span class="js-op">{</span>
<span class="js-id">int</span> <span class="js-id">integerValue</span><span class="js-op">;</span>
<span class="js-id">float</span> <span class="js-id">floatValue</span><span class="js-op">;</span>
<span class="js-id">char</span> <span class="js-id">characterValue</span><span class="js-op">;</span>
<span class="js-op">}</span> <span class="js-id">data</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">Value</span> <span class="js-id">value</span><span class="js-op">;</span>

<span class="js-id">value</span><span class="js-op">.</span><span class="js-id">type</span> <span class="js-op">=</span> <span class="js-num">1</span><span class="js-op">;</span>
<span class="js-id">value</span><span class="js-op">.</span><span class="js-id">data</span><span class="js-op">.</span><span class="js-id">integerValue</span> <span class="js-op">=</span> <span class="js-num">100</span><span class="js-op">;</span>

<span class="js-kw">if</span> <span class="js-op">(</span><span class="js-id">value</span><span class="js-op">.</span><span class="js-id">type</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-id">cout</span> <span class="js-op">&lt;&lt;</span> <span class="js-str">"Integer: "</span> <span class="js-op">&lt;&lt;</span> <span class="js-id">value</span><span class="js-op">.</span><span class="js-id">data</span><span class="js-op">.</span><span class="js-id">integerValue</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>Output:</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">Integer</span><span class="js-op">:</span> <span class="js-num">100</span></code></pre></div><p>Explanation:</p><ul><li><p><code>type</code> identifies the active value type.</p></li><li><p><code>data</code> stores the selected value in shared memory.</p></li><li><p>The program checks <code>type</code> before reading the corresponding union member.</p></li></ul><p>The programmer must keep the tag and active union member consistent.</p><p>For many applications, <code>std::variant</code> is a safer alternative because it tracks the currently held type automatically.</p><h2>13. Difference Between Structure and Union in C++</h2><p>Structures and unions are both user-defined data types, but their memory organization is different.</p><table><tbody><tr><th>Feature</th><th>Structure (<code>struct</code>)</th><th>Union (<code>union</code>)</th></tr><tr><td>Memory allocation</td><td>Separate storage for each non-static member</td><td>Members share storage</td></tr><tr><td>Member values</td><td>All members can hold independent values</td><td>Normally one active member at a time</td></tr><tr><td>Size</td><td>Includes storage for all members and padding</td><td>Sufficient for the largest member and alignment</td></tr><tr><td>Access</td><td>Dot or arrow operator</td><td>Dot or arrow operator</td></tr><tr><td>Use case</td><td>Grouping related data</td><td>Alternative values sharing storage</td></tr><tr><td>Member initialization</td><td>Supports multiple members</td><td>Initializes an active member</td></tr></tbody></table><h3>Example: Structure</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">struct</span> <span class="js-id">Data</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-id">float</span> <span class="js-id">decimal</span><span class="js-op">;</span>
<span class="js-id">char</span> <span class="js-id">letter</span><span class="js-op">;</span>
<span class="js-op">};</span></code></pre></div><p>All three members can hold independent values at the same time.</p><h3>Example: Union</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">union</span> <span class="js-id">Data</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-id">float</span> <span class="js-id">decimal</span><span class="js-op">;</span>
<span class="js-id">char</span> <span class="js-id">letter</span><span class="js-op">;</span>
<span class="js-op">};</span></code></pre></div><p>The members share storage, so the union is generally used for one active value at a time.</p><h2>14. Union vs std::variant in Modern C++</h2><p>C++17 introduced <code>std::variant</code>, which provides a type-safe way to store one value from a list of alternative types.</p><p>Unlike a raw union, <code>std::variant</code> tracks which alternative is currently active and manages the lifetime of its contained value.</p><h3>Example: Using std::variant</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">include</span> <span class="js-op">&lt;</span><span class="js-id">variant</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">variant</span><span class="js-op">&lt;</span><span class="js-id">int</span><span class="js-op">,</span> <span class="js-id">float</span><span class="js-op">,</span> <span class="js-id">string</span><span class="js-op">&gt;</span> <span class="js-id">value</span><span class="js-op">;</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">cout</span> <span class="js-op">&lt;&lt;</span> <span class="js-id">get</span><span class="js-op">&lt;</span><span class="js-id">int</span><span class="js-op">&gt;(</span><span class="js-id">value</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">value</span> <span class="js-op">=</span> <span class="js-num">25</span><span class="js-op">.</span>5f<span class="js-op">;</span>
<span class="js-id">cout</span> <span class="js-op">&lt;&lt;</span> <span class="js-id">get</span><span class="js-op">&lt;</span><span class="js-id">float</span><span class="js-op">&gt;(</span><span class="js-id">value</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">value</span> <span class="js-op">=</span> <span class="js-id">string</span><span class="js-op">(</span><span class="js-str">"Hello C++"</span><span class="js-op">);</span>
<span class="js-id">cout</span> <span class="js-op">&lt;&lt;</span> <span class="js-id">get</span><span class="js-op">&lt;</span><span class="js-id">string</span><span class="js-op">&gt;(</span><span class="js-id">value</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>Output:</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">25.5</span>
<span class="js-id">Hello</span> <span class="js-id">C</span><span class="js-op">++</span></code></pre></div><p><code>std::variant</code> is often a better choice when you need a value that can hold one of several different types safely.</p><table><tbody><tr><th>Feature</th><th>Union</th><th>std::variant</th></tr><tr><td>Introduced</td><td>Available in C++</td><td>C++17</td></tr><tr><td>Shared storage</td><td>Yes</td><td>Uses storage for its alternatives</td></tr><tr><td>Tracks active type</td><td>Not automatically</td><td>Yes</td></tr><tr><td>Type safety</td><td>Requires careful management</td><td>Type-safe access</td></tr><tr><td>Complex member types</td><td>Requires careful lifetime management</td><td>Supports suitable non-trivial types</td></tr><tr><td>Typical use</td><td>Low-level storage and specialized layouts</td><td>Type-safe alternative values</td></tr></tbody></table><h2>15. Real-World Applications of Unions in C++</h2><p>Unions can be useful in applications where memory layout and alternative data representations matter.</p><h3>Embedded Systems</h3><p>Embedded applications may use unions to represent different interpretations of data while working with constrained memory.</p><h3>Hardware and Device Programming</h3><p>Low-level code may use unions when interacting with hardware representations, provided the implementation and hardware requirements are properly understood.</p><h3>Network Protocols</h3><p>A data field may represent different types of information depending on a message type or protocol tag.</p><h3>Compilers and Interpreters</h3><p>A value representation may need to store one of several possible data types.</p><h3>Memory-Constrained Applications</h3><p>Unions can reduce storage requirements when only one of several alternative values needs to be active at a time.</p><p><strong>Note:</strong> Unions do not automatically make a program faster. Choose them when their shared-storage behavior is genuinely useful.</p><h2>16. Common Mistakes When Using Unions</h2><h3>Mistake 1: Reading an Inactive Member</h3><p>Incorrect:</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">union</span> <span class="js-id">Data</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-id">float</span> <span class="js-id">decimal</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">Data</span> <span class="js-id">value</span><span class="js-op">;</span>

<span class="js-id">value</span><span class="js-op">.</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">cout</span> <span class="js-op">&lt;&lt;</span> <span class="js-id">value</span><span class="js-op">.</span><span class="js-id">decimal</span><span class="js-op">;</span> <span class="js-cm">// Generally not valid</span>
<span class="js-op">}</span></code></pre></div><p>The integer member is active. Reading the floating-point member this way is not generally permitted by the C++ object-lifetime rules.</p><p>Read the active member instead.</p><h3>Mistake 2: Assuming All Members Have Separate Values</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">union</span> <span class="js-id">Data</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-id">float</span> <span class="js-id">decimal</span><span class="js-op">;</span>
<span class="js-op">};</span></code></pre></div><p>You should not treat <code>number</code> and <code>decimal</code> as independent storage locations. They overlap.</p><h3>Mistake 3: Forgetting to Track the Active Member</h3><p>When a union is used inside a larger structure, the program should keep track of which member is active.</p><p>Without that information, code may accidentally read the wrong member.</p><h3>Mistake 4: Ignoring Member Lifetime Rules</h3><p>Unions containing types with non-trivial constructors or destructors require careful lifetime management.</p><p>For complex alternative types, consider <code>std::variant</code> instead.</p><h3>Mistake 5: Assuming the Union Size Is the Sum of Its Members</h3><p>A union does not normally allocate separate storage for every member. Its size is based on its largest member and alignment requirements, not the sum of all member sizes.</p><h2>17. Best Practices for Using Unions</h2><ol start="1"><li><p>Use unions only when shared storage is genuinely required.</p></li><li><p>Keep track of the currently active member.</p></li><li><p>Never read an inactive member unless a specific C++ language rule permits it.</p></li><li><p>Use <code>std::variant</code> when you need type-safe alternative values.</p></li><li><p>Be careful with unions containing complex types such as <code>std::string</code>.</p></li><li><p>Understand object lifetime and construction rules before using non-trivial union members.</p></li><li><p>Avoid relying on platform-specific memory layouts unless the application requires them.</p></li><li><p>Test low-level union code with the intended compiler and target platform.</p></li><li><p>Document which member is active and how that state is maintained.</p></li><li><p>Prefer simpler structures or standard library types when shared storage is unnecessary.</p></li></ol><h2>18. Frequently Asked Questions (FAQs)</h2><h3>1. What is a union in C++?</h3><p>A union is a user-defined data type in which all non-static data members share the same memory location.</p><h3>2. Which keyword is used to create a union?</h3><p>The <code>union</code> keyword is used to declare a union in C++.</p><h3>3. How is a union different from a structure?</h3><p>A structure provides separate storage for its non-static data members, while union members share storage.</p><h3>4. Can a union contain different data types?</h3><p>Yes. A union can contain members with different data types, such as <code>int</code>, <code>float</code>, <code>char</code>, and other suitable types.</p><h3>5. Can all union members store independent values at the same time?</h3><p>No. A union normally has one active member at a time because its members share the same storage.</p><h3>6. How do you access union members?</h3><p>Use the dot (<code>.</code>) operator with a union object or the arrow (<code>-&gt;</code>) operator with a pointer.</p><h3>7. How do you find the size of a union?</h3><p>Use the <code>sizeof</code> operator:</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">sizeof</span><span class="js-op">(</span><span class="js-id">Data</span><span class="js-op">);</span></code></pre></div><p>The result depends on the largest member and the alignment requirements.</p><h3>8. Can we create an array of unions?</h3><p>Yes. You can declare an array of union objects:</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">Data</span> <span class="js-id">values</span>[<span class="js-num">5</span>]<span class="js-op">;</span></code></pre></div><p>Each element has its own union storage.</p><h3>9. Can a union contain a structure?</h3><p>Yes. A union can contain a structure as one of its members, subject to the normal member lifetime rules.</p><h3>10. Can we pass a union to a function?</h3><p>Yes. You can pass a union by value, by reference, or by constant reference.</p><h3>11. Can a union contain a string in C++?</h3><p>Yes, but a <code>std::string</code> is a non-trivial type. Managing its construction, destruction, and active-member lifetime inside a union requires care. <code>std::variant</code> is often a safer option.</p><h3>12. What is std::variant?</h3><p><code>std::variant</code> is a C++17 standard library type that stores one value from several alternative types and tracks which type is currently active.</p>

Code Example

#include <iostream>
#include <variant>
#include <string>

// Type-safe Modern C++17 Variant
using ConfigValue = std::variant<int, double, std::string>;

int main() {
    ConfigValue setting = 404; // Holds int
    std::cout << "Integer value: " << std::get<int>(setting) << "
";

    setting = std::string("https://operatetools.com/api"); // Holds string
    std::cout << "String value:  " << std::get<std::string>(setting) << "
";

    // Pattern matching with std::visit
    std::visit([](const auto& val) {
        std::cout << "Visited active value: " << val << "
";
    }, setting);

    return 0;
}

Interview Question & Tips

Q: What happens if you read a union member that was not the last one written?
A: In C++, accessing a union member other than the active member is undefined behavior (type punning), except for standard-layout unions with common initial sequences.

Q: Why is std::variant preferred over raw unions in modern C++?
A: `std::variant` automatically tracks the active type index, handles non-trivial constructors/destructors safely, and prevents memory corruption by throwing exceptions on invalid type access.

Best Practices

1. Prefer `std::variant` (C++17) over raw `union` for type-safe polymorphic data holding.
2. Use raw unions only for low-level hardware registers, binary protocol decoding, or micro-optimizations.
3. Use `std::holds_alternative<T>(variant)` or `std::visit` to inspect variant data safely.
Academic Standards & Verified Citations

This study reference for C++ Unions: Shared Memory Storage and Practical Examples is curated and verified against recognized curriculum benchmarks, peer-reviewed textbooks, and official technical documentation:

C++ Programming