{"id":948,"date":"2014-09-13T00:01:21","date_gmt":"2014-09-13T07:01:21","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=948"},"modified":"2014-09-20T08:39:49","modified_gmt":"2014-09-20T15:39:49","slug":"breaking-down-structures","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=948","title":{"rendered":"Breaking Down Structures"},"content":{"rendered":"<p>In the C language, <code>struct<\/code> is a variable type, but do you <em>declare<\/em> a structure or <em>define<\/em> a structure? That one has me perplexed.<br \/>\n<!--more--><br \/>\nRegardless of the nomenclature, a structure is a collection of variable types, like a record in a database. You can pack any type of variable into a structure &#8212; including more structures. The idea is that the information within the structure is related and somehow serves a useful purpose by being referenced as a unit.<\/p>\n<p>A simple example is a structure that holds a date value:<\/p>\n<pre><code>struct date {\r\n    int year;\r\n    int month;\r\n    int day;\r\n};<\/code><\/pre>\n<p>The structure is named <code>date<\/code>. It holds three members (not elements), all <em>int<\/em> variables.<\/p>\n<p>Defining a structure doesn&#8217;t create a structure variable. It merely tells the compiler, &#8220;Hey! Here&#8217;s a new type of structure. Maybe I&#8217;ll use it.&#8221; To declare a structure variable, you use this format:<\/p>\n<p><code>struct date birthday;<\/code><\/p>\n<p>The variable <code>birthday<\/code> uses the <code>date<\/code> structure type. It&#8217;s assumed that the <code>date<\/code> structure is defined elsewhere.<\/p>\n<p>Here&#8217;s sample code:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    struct date {\r\n        int year;\r\n        int month;\r\n        int day;\r\n    };\r\n    struct date birthday;\r\n\r\n    birthday.year = 1935;\r\n    birthday.month = 7;\r\n    birthday.day = 6;\r\n\r\n    printf(\"The Dalai Lama was born on %d\/%02d\/%d.\\n\",\r\n            birthday.month,\r\n            birthday.day,\r\n            birthday.year);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>To access a structures members, you use the structure variable name (not the structure name). So the sample code uses <code>birthday<\/code> not <code>date<\/code>. Then you use a dot and the structure member name.<\/p>\n<p>Here&#8217;s sample output:<\/p>\n<pre><code>The Dalai Lama was born on 7\/06\/1935.<\/code><\/pre>\n<p>Hopefully all this info is familiar to you if you&#8217;ve read my books. If not, it always helps to brush up.<\/p>\n<p>One thing other programmers do that I don&#8217;t is combine the structure definition and the variable declaration into a single statement. It looks like this:<\/p>\n<pre><code>struct date {\r\n    int year;\r\n    int month;\r\n    int day;\r\n} birthday;<\/code><\/pre>\n<p>This statement (it&#8217;s one statement split over 5 lines) both defines the <code>date<\/code> structure and declares a <code>date<\/code> structure variable, <code>birthday<\/code>. You may prefer this method, which is fine. I expand it out to two statements, which does the same thing but it makes structures easier to teach.<\/p>\n<p>For me, the most difficult part of using structures is coming up with clever names: one for the structure itself and another for the structure variable. Generally speaking, I can come up with a single clever name for either the structure or the variable, but not both. I&#8217;ll continue to work on that.<\/p>\n<p>In <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=950\">next week&#8217;s Lesson<\/a>, I&#8217;ll review nested structures.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Fear not the <code>struct<\/code> variable. It can be fun and useful. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=948\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-948","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\/948","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=948"}],"version-history":[{"count":4,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/948\/revisions"}],"predecessor-version":[{"id":998,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/948\/revisions\/998"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=948"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=948"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=948"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}