{"id":950,"date":"2014-09-20T00:01:35","date_gmt":"2014-09-20T07:01:35","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=950"},"modified":"2014-09-13T08:36:55","modified_gmt":"2014-09-13T15:36:55","slug":"strucstructurestures","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=950","title":{"rendered":"Struc(Structures)tures"},"content":{"rendered":"<p>A structure is capable of containing any other type of C language variable. That includes other structures.<br \/>\n<!--more--><br \/>\nThe structure-within-a-structure thing is kind of odd. It&#8217;s not really necessary unless you have a single structure that&#8217;s used twice, for instance, to reference the same type of data.<\/p>\n<p>To better explain how nested structures work, consider the following code:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;string.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 called {\r\n        char first[32];\r\n        char last[32];\r\n    };\r\n    struct human {\r\n        struct date birthday;\r\n        struct called name;\r\n    };\r\n    struct human president;     <span class=\"comments\">\/* declare structure variable *\/<\/span>\r\n\r\n<span class=\"comments\">\/* fill structure data *\/<\/span>\r\n    strcpy(president.name.first,\"George\");\r\n    strcpy(president.name.last,\"Washington\");\r\n    president.birthday.year = 1732;\r\n    president.birthday.month = 2;\r\n    president.birthday.day = 22;\r\n\r\n<span class=\"comments\">\/* display results *\/<\/span>\r\n    printf(\"President %s %s was born on %d\/%02d\/%d.\\n\",\r\n            president.name.first,\r\n            president.name.last,\r\n            president.birthday.month,\r\n            president.birthday.day,\r\n            president.birthday.year);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>This code uses two structures, <code>date<\/code> and <code>called<\/code>, and creates a third structure <code>human<\/code>. The <code>human<\/code> structure contains the nested <code>date<\/code> and <code>called<\/code> structures by using the variables <code>birthday<\/code> and <code>name<\/code>, respectively. This method should be familiar to you from my books.<\/p>\n<p>Here&#8217;s sample output:<\/p>\n<pre><code>President George Washington was born on 2\/22\/1732.<\/code><\/pre>\n<p>The problem here is that no pressing need demands the nested structures. They don&#8217;t solve any problems because the entire structure could be rewritten as:<\/p>\n<pre><code>struct human {\r\n    int birthyear;\r\n    int birthmonth;\r\n    int birthday;\r\n    char firstname[32];\r\n    char lastname[32];\r\n};<\/code><\/pre>\n<p>Other, obvious changes would be required in the code to reflect this new structure, but my point is that nothing is gained by using nested structures. The following code, however, demonstrates how the nested structures could be used:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;string.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 called {\r\n        char first[32];\r\n        char last[32];\r\n    };\r\n    struct human {\r\n        struct called name;\r\n        struct date birthday;\r\n        struct date married;\r\n        struct called spouse;\r\n    };\r\n    struct human president;     <span class=\"comments\">\/* declare structure variable *\/<\/span>\r\n\r\n<span class=\"comments\">\/* fill structure data *\/<\/span>\r\n    strcpy(president.name.first,\"George\");\r\n    strcpy(president.name.last,\"Washington\");\r\n    president.birthday.year = 1732;\r\n    president.birthday.month = 2;\r\n    president.birthday.day = 22;\r\n    strcpy(president.spouse.first,\"Martha\");\r\n    strcpy(president.spouse.last,\"Curtis\");\r\n    president.married.year = 1759;\r\n    president.married.month = 1;\r\n    president.married.day = 6;\r\n\r\n<span class=\"comments\">\/* display results *\/<\/span>\r\n    printf(\"President %s %s was born on %d\/%02d\/%d.\\n\",\r\n            president.name.first,\r\n            president.name.last,\r\n            president.birthday.month,\r\n            president.birthday.day,\r\n            president.birthday.year);\r\n    printf(\"He married %s %s on %d\/%02d\/%d.\\n\",\r\n            president.spouse.first,\r\n            president.spouse.last,\r\n            president.married.month,\r\n            president.married.day,\r\n            president.married.year);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>In the code above, the two structures <code>date<\/code> and <code>called<\/code> are used twice within the <code>human<\/code> structure. That&#8217;s really how nested structures are useful, when they represent the same type of data but for two different purposes. In this case, both birthday and marriage day.<\/p>\n<p>Here&#8217;s sample output:<\/p>\n<pre><code>President George Washington was born on 2\/22\/1732.\r\nHe married Martha Curtis on 1\/06\/1759.<\/code><\/pre>\n<p>Coming up next Lesson: pointers to structures. Yum!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Structures within structures. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=950\">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-950","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\/950","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=950"}],"version-history":[{"count":7,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/950\/revisions"}],"predecessor-version":[{"id":975,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/950\/revisions\/975"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=950"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=950"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=950"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}