{"id":7775,"date":"2026-09-19T00:01:09","date_gmt":"2026-09-19T07:01:09","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=7775"},"modified":"2026-09-12T12:31:42","modified_gmt":"2026-09-12T19:31:42","slug":"unions-and-data-storage","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=7775","title":{"rendered":"Unions and Data Storage"},"content":{"rendered":"<p>A C language keyword worthy of shunning is <em>union<\/em>. It joins <em>goto<\/em> as another keyword to avoid, but <em>union<\/em> is more in the realm of the deprecated <em>gets()<\/em> function in that it has a potential for dangerous exploits. So why not have some fun with it before the C Lords banish it from the language?<br \/>\n<!--more--><br \/>\nNo, I have no insight as to whether the <em>union<\/em> keyword will be banished in the next C language update. It is, however, on the list of things to avoid as it can be exploited as a weakness.<\/p>\n<p>I&#8217;ve written about the <em>union<\/em> keyword <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=1449\">before<\/a>. As a summary, it&#8217;s a data container similar to a structure in its declaration. Like the structure, a union contains identifiers. But unlike a structure, the union&#8217;s identifiers share the same data space. So whereas a structure hosts multiple members of different types each with its own space, a union holds a multiple members of different types sharing a single space. It&#8217;s the single space part that&#8217;s dangerous: You can set data of one type into the union and access it via another type. Such data typing inconsistency isn&#8217;t present in modern programming languages nor is there any compiler oversight regarding how the data is interpreted.<\/p>\n<p>The following code illustrates a union with two members, an integer and a character:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2026_09_19-Lesson.c\" rel=\"noopener\" target=\"_blank\">2026_09_19-Lesson.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    union stow {\r\n        int l;\r\n        char c;\r\n    } s;\r\n\r\n    s.l = 0x87654321;\r\n    printf(\"Union 's' stores: 0x%X\\n\",s.l);\r\n    printf(\"But also: %c\\n\",s.c);\r\n\r\n    return 0;\r\n}<\/pre>\n<p>The union <em>stow<\/em> contains two members: <em>int<\/em> <code>l<\/code> and <em>char<\/em> <code>c<\/code>. Variable <code>s<\/code> is created as the <em>union<\/em> is declared.<\/p>\n<p>Hex value 0x87654321 is assigned to member <code>s.l<\/code>. The first <em>printf()<\/em> statement outputs this value. The second <em>printf()<\/em> statement accesses member <em>s.c<\/em>, the character, and outputs its value. Both member use the same data stored in the union&#8217;s space to generate this output:<\/p>\n<pre>Union 's' stores: 0x87654321\r\nBut also: !<\/pre>\n<p>The storage space for union <em>stow<\/em> is large enough to contain an integer value, 32 bits. Figure 1 illustrates how this storage may look.<\/p>\n<div id=\"attachment_7776\" style=\"width: 460px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-7776\" src=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2026\/09\/0919-figure1.png\" alt=\"a union&#039;s memory map\" width=\"450\" height=\"239\" class=\"size-full wp-image-7776\" srcset=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2026\/09\/0919-figure1.png 450w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2026\/09\/0919-figure1-300x159.png 300w\" sizes=\"auto, (max-width: 450px) 100vw, 450px\" \/><p id=\"caption-attachment-7776\" class=\"wp-caption-text\">Both members of the union share the storage space, but access only a relevant part.<\/p><\/div>\n<p>Integer <code>s.l<\/code> stores its full value in the space allocated for the union, four bytes. Character <code>s.c<\/code> uses only one of those bytes. Because the values share the same storage space, 0x21 is assigned to <code>s.c<\/code> when 0x87654321 is set for member <code>s.l<\/code>.<\/p>\n<p>To confirm the union&#8217;s size, I added this line to the code:<\/p>\n<p><code>printf(\"Union 's' is %lu bytes\\n\",sizeof(s));<\/code><\/p>\n<p>Here is the updated output:<\/p>\n<pre>Union 's' stores: 0x87654321\r\nBut also: !\r\nUnion 's' is 4 bytes<\/pre>\n<p>The <em>sizeof<\/em> operator confirms that union <code>s<\/code> occupies four bytes of storage, the size of an <em>int<\/em>, its largest member.<\/p>\n<p>Next week I continue my exploration of the union, with an example of unions were once an important part of C programming back in the early PC days.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The union is an odd (and dangerous) data structure, but its character lends itself to some useful info spelunking. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=7775\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-7775","post","type-post","status-publish","format-standard","hentry","category-main"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/7775","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=7775"}],"version-history":[{"count":5,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/7775\/revisions"}],"predecessor-version":[{"id":7790,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/7775\/revisions\/7790"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7775"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7775"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7775"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}