{"id":4441,"date":"2013-09-26T05:14:39","date_gmt":"2013-09-25T19:14:39","guid":{"rendered":"http:\/\/www.rjmprogramming.com.au\/wordpress\/?p=4441"},"modified":"2013-09-26T05:14:39","modified_gmt":"2013-09-25T19:14:39","slug":"c-union-primer-tutorial","status":"publish","type":"post","link":"https:\/\/www.rjmprogramming.com.au\/ITblog\/c-union-primer-tutorial\/","title":{"rendered":"C Union Primer Tutorial"},"content":{"rendered":"<div style=\"width: 230px\" class=\"wp-caption alignnone\"><a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/C\/union\/\"><img decoding=\"async\" style=\"float:left; border: 15px solid pink;\" alt=\"C Union Primer Tutorial\" src=\"http:\/\/www.rjmprogramming.com.au\/C\/union\/C_union_Primer-10of.jpg\" title=\"C Union Primer Tutorial\"  \/><\/a><p class=\"wp-caption-text\">C Union Primer Tutorial<\/p><\/div>\n<p>The C programming language introduced a data structure called union which allowed that data structure to contain a range of different data types which could come and go in relevance, during program execution, for many and various purposes, as the programmer saw fit.   Perhaps think of it like an artist&#8217;s palette or the HTML5 element canvas (or the canvas of Tcl\/TK).    Union can be used in C++ as well, as this tutorial intimates, as Xcode (on a Mac) is by default a C++ environment, which tolerates a C compilation if you code your code in the proper way.    Today&#8217;s tutorial also uses some other concepts, one of which hopefully of interest, is the use of a bit field.   In making this tutorial a topic of change between C++ and C came about to do with the use of enum and trying to work out the bit offset of a bit field within a bit field struct.   Wanted to use a generic approach, and the closest is a macro called offsetof in stddef.h but found that various platforms and the issue of wanting the same codebase for C and C++ precluded its use &#8230; you might want to read more <a target=_blank title='offsetof' href='http:\/\/www.cplusplus.com\/reference\/cstddef\/offsetof\/'>here<\/a>.<\/p>\n<p><b>Background reading for tutorial (all links by <a target=_blank title='tutorialspoint' href='http:\/\/www.tutorialspoint.com\/cprogramming\/'>tutorialspoint<\/a> &#8230; thanks):<\/p>\n<ul>\n<li><a target=_blank title='union' href='http:\/\/www.tutorialspoint.com\/cprogramming\/c_unions.htm'>union<\/a><\/li>\n<li><a target=_blank title='struct' href='http:\/\/www.tutorialspoint.com\/cprogramming\/c_structures.htm'>struct<\/a><\/li>\n<li><a target=_blank title='enum' href='http:\/\/www.tutorialspoint.com\/ansi_c\/c_using_constants.htm'>enum<\/a><\/li>\n<li><a target=_blank title='bit field' href='http:\/\/www.tutorialspoint.com\/cprogramming\/c_bit_fields.htm'>bit field<\/a><\/li>\n<li><a target=_blank title='typedef' href='http:\/\/www.tutorialspoint.com\/cprogramming\/c_typedef.htm'>typedef<\/a><\/li>\n<\/ul>\n<p><\/b>\n<\/p>\n<p>Here is a tutorial that introduces you to some C programming code for the use of a union using Xcode, (at the end of tutorial is shown running in a Linux Mac OS X Terminal (application) bash environment) &#8230;<\/p>\n<p>Let&#8217;s see some <a target=_blank title='click picture' href='http:\/\/www.rjmprogramming.com.au\/C\/union\/'>C via XCode<\/a> for this &#8230;<\/p>\n<p>Link to some downloadable code &#8230; rename to <a target=_blank href='http:\/\/www.rjmprogramming.com.au\/C\/union\/main.cpp_GETME' title='Download me'>main.cpp<\/a>  (or main.c &#8230; see how XCode can do C as well!) for use.<\/p>\n<p>Regarding C programming generally really like <em>\u201cUsing ANSI C in UNIX\u201d<\/em> by <em>Werner Feibel<\/em>.<\/p>\n<p>If this was interesting you may be interested in <a title='Click here to see topics in which you might be interested' href='#d4441' onclick='var dv=document.getElementById(\"d4441\"); dv.innerHTML = \"&lt;iframe width=670 height=600 src=\" + \"http:\/\/www.rjmprogramming.com.au\/wordpress\/?s=main.c\" + \"&gt;&lt;\/iframe&gt;\"; dv.style.display = \"block\";'>this<\/a> too.<\/p>\n<div id='d4441' style='display: none; border-left: 2px solid green; border-top: 2px solid green;'><\/div>\n","protected":false},"excerpt":{"rendered":"<p>The C programming language introduced a data structure called union which allowed that data structure to contain a range of different data types which could come and go in relevance, during program execution, for many and various purposes, as the &hellip; <a href=\"https:\/\/www.rjmprogramming.com.au\/ITblog\/c-union-primer-tutorial\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12,37,40],"tags":[132,147,176,390,707,730,997,1207,1252,1319,1327,1334,1473],"class_list":["post-4441","post","type-post","status-publish","format-standard","hentry","category-elearning","category-tutorials","category-xcode","tag-bash","tag-bit-field","tag-c","tag-enum","tag-linux","tag-mac-osx","tag-programming","tag-struct","tag-terminal","tag-tutorial","tag-typedef","tag-union","tag-xcode"],"_links":{"self":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts\/4441"}],"collection":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/comments?post=4441"}],"version-history":[{"count":0,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts\/4441\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/media?parent=4441"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/categories?post=4441"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/tags?post=4441"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}