{"id":7795,"date":"2026-10-03T00:01:08","date_gmt":"2026-10-03T07:01:08","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=7795"},"modified":"2026-09-29T08:53:44","modified_gmt":"2026-09-29T15:53:44","slug":"the-scary-part-of-the-union","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=7795","title":{"rendered":"The Scary Part of the Union"},"content":{"rendered":"<p>From <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=7784&#038;preview=true\">last week&#8217;s Lesson<\/a>, you saw an example of how different data types stored in a union can be beneficial. As both structures <code>WORDREGS<\/code> and <code>BYTEREGS<\/code> overlapped to reflect the data stored in an 8086 CPU, using a union to represent the data is both practical and economic. But such conditions are rare.<br \/>\n<!--more--><br \/>\nThe union in the following code holds two 8-byte chunks of data. One is identified as a string (or character aarray) and the other as a <em>long<\/em> integer.<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2026_10_03-Lesson-a.c\" rel=\"noopener\" target=\"_blank\">2026_10_03-Lesson-a.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    union text {\r\n        char ch[8];\r\n        long li;\r\n    } t;\r\n\r\n    t.li = 0xA216F6C6C6548;\r\n    printf(\"%s\",t.ch);\r\n\r\n    return 0;\r\n}<\/pre>\n<p>The <code>text<\/code> <em>union<\/em> <code>t<\/code> stores string <code>ch<\/code> and <em>long int<\/em> <code>li<\/code>. The value of <code>t.li<\/code> is assigned 0xA216F6C6C6548. But the <em>printf()<\/em> statement outputs string <code>ch<\/code>, which is based on the data stored in <code>t.li<\/code>. Here&#8217;s the program&#8217;s output:<\/p>\n<pre>Hello!<\/pre>\n<p>The value of <code>t.li<\/code> is equal to the ASCII codes for the text stored in string <code>ch<\/code>. This relationship may not be immediately apparent as the characters are read in reverse order, which is the way values are stored byte-wise inside the computer:<\/p>\n<p>00 = null character<br \/>\n0A = newline<br \/>\n21 = &#8216;!&#8217;<br \/>\n6F = &#8216;o&#8217;<br \/>\n6C = &#8216;l&#8217;<br \/>\n6C = &#8216;l&#8217;<br \/>\n65 = &#8216;e&#8217;<br \/>\n48 = &#8216;H&#8217;<\/p>\n<p>Now consider this code:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2026_10_03-Lesson-b.c\" rel=\"noopener\" target=\"_blank\">2026_10_03-Lesson-b.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    union text {\r\n        char ch[8];\r\n        long li;\r\n    } t;\r\n\r\n    t.li = 0xA216F6C6C6548;\r\n    printf(\"%s\",t.ch);\r\n    t.li -= 0xAFFF9F5F1;\r\n    printf(\"%s\",t.ch);\r\n\r\n    return 0;\r\n}<\/pre>\n<p>In this update, the value 0xAFFF9F5F1 is subtracted from <code>t.li<\/code>. The result is output as another string:<\/p>\n<pre>Hello!\r\nWorld!<\/pre>\n<p>The result is amusing, but consider how the data modified in of one type but affects the other type. This relationship shows the danger of using a union to store data, especially unrelated data. Even when the motive in intentional and the data types are related to each other in some way, the results can be unpredictable. But there is a solution.<\/p>\n<p>The answer is to <em>tag<\/em> a union. I cover this process in next week&#8217;s Lesson.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Security is an issue for a union as its data is shared between different types. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=7795\">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-7795","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\/7795","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=7795"}],"version-history":[{"count":4,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/7795\/revisions"}],"predecessor-version":[{"id":7816,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/7795\/revisions\/7816"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7795"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7795"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7795"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}