{"id":1449,"date":"2015-07-18T00:01:55","date_gmt":"2015-07-18T07:01:55","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=1449"},"modified":"2015-07-20T08:57:14","modified_gmt":"2015-07-20T15:57:14","slug":"to-form-a-more-perfect-union","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=1449","title":{"rendered":"To Form a More Perfect <em>union<\/em>"},"content":{"rendered":"<p>Next to <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=1440\"><em>enum<\/em><\/a>, one of the more curious C language keywords is <em>union<\/em>. It&#8217;s tremendously unpopular. I would offer that it&#8217;s also not needed, but no one is talking about deprecating it.<br \/>\n<!--more--><br \/>\nThe <em>union<\/em> keyword has its roots in the Pascal language. Pascal used something called a <em>variant record<\/em>, which was a container into which you could place different types of variables. The <em>union<\/em> keyword lets you create a similar type of container, although for the life of me I can&#8217;t describe a situation where it&#8217;s the only solution.<\/p>\n<p>When you create a union variable, you specify multiple variable types it can contain. For example, a union could contain a <em>float<\/em> or an <em>int<\/em> value. Or it could contain two or more structures. The compiler allocates space for the widest variable. After that, it&#8217;s up to the programmer to determine which variable type is used and access it appropriately.<\/p>\n<blockquote><p>The C language union is yet another example of how C is considered dangerous by modern standards. In other languages, you can&#8217;t plop down storage for an any-type variable. The compiler requires that the variable types always be known and strictly adhered to.<\/p><\/blockquote>\n<p>As you know, variables in C are allocated storage based on their type. A <em>char<\/em> is one byte, an <em>int<\/em> could be 4 bytes on some machines, and so on. The <em>sizeof<\/em> operator returns the actual variable size.<\/p>\n<p>When you define a union in your code, it lists several potential variable types. The compiler allocates storage for only one variable by using the widest type. So if the union consists of a single <em>char<\/em> and a <em>float<\/em>, the union will be the size of the <em>float<\/em>, the wider variable.<\/p>\n<p>If that&#8217;s not confusing enough, unions are declared similar to a structure. In fact, I could argue that a structure would be a better tool and I&#8217;m sure many other programmers simply come to that conclusion.<\/p>\n<p>The following declaration is for a union that can hold either a <em>char<\/em> or a <em>float<\/em>:<\/p>\n<pre><code>union jack {\r\n    char c;\r\n    float f;\r\n} u;<\/code><\/pre>\n<p>The <em>union<\/em> is named <code>jack<\/code>, similar to how a structure type is declared. The <code>jack<\/code> union can hold either a <em>char<\/em> or <em>float<\/em> variable, named <code>c<\/code> or <code>f<\/code>, respectively. The variable <code>u<\/code> is the name of the <code>jack<\/code> union.<\/p>\n<p>Unlike a structure, the union doesn&#8217;t contain both the <em>char<\/em> and <em>float<\/em>. Storage is available for only one. So you can use either <code>u.c<\/code> for the <em>char<\/em> variable or <code>u.f<\/code> for the <em>float<\/em>, but not both.<\/p>\n<p>So what&#8217;s the point of a <em>union<\/em>?<\/p>\n<p>Seriously, you got me.<\/p>\n<p>Back in the dark days of DOS, programmers used a union of structures to set the PC&#8217;s CPU registers. The union allowed access to the registers as both 8-bit bytes and 16-bit words, which came in handy. Since then, I&#8217;ve not seen any code where a union was required.<\/p>\n<p>I&#8217;ve concocted the following source code as an example of how a union could be used:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    union length {\r\n        float fsize;\r\n        int isize;\r\n    };\r\n    union length l;\r\n\r\n    l.fsize = 2.09;\r\n    printf(\"The table is exactly %.2f meters long.\\n\",\r\n            l.fsize);\r\n    l.isize = 2;\r\n    printf(\"The table is about %d meters long.\\n\",\r\n            l.isize);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>I split the union declaration into two parts, similar to how I declare structures. The union <code>length<\/code> is defined at Lines 5 through 8. Line 9 declares the <code>length<\/code> union variable <code>l<\/code>.<\/p>\n<p>At Line 11, the union&#8217;s <em>float<\/em> member is accessed, <code>l.fsize<\/code>. It&#8217;s used in the <em>printf()<\/em> function at Lines 12 and 13.<\/p>\n<p>At Line 14, the union&#8217;s <em>int<\/em> member is accessed, <code>l.isize<\/code>. It&#8217;s the same memory as <code>l.fsize<\/code>, but a different type thanks to the union.<\/p>\n<p>Here&#8217;s sample output:<\/p>\n<pre><code>The table is exactly 2.09 meters long.\r\nThe table is about 2 meters long.<\/code><\/pre>\n<p>I confess that this example is silly, mostly because this isn&#8217;t the only solution available. If I discover an absolute scenario where a C language union is either indispensable or used in a wildly inventive way, I&#8217;ll pass it along in a future Lesson.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Another of the C language&#8217;s mystery keywords unveiled. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=1449\">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-1449","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\/1449","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=1449"}],"version-history":[{"count":4,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1449\/revisions"}],"predecessor-version":[{"id":1482,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1449\/revisions\/1482"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1449"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1449"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1449"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}