{"id":2708,"date":"2017-09-23T00:01:32","date_gmt":"2017-09-23T07:01:32","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=2708"},"modified":"2017-09-16T10:05:04","modified_gmt":"2017-09-16T17:05:04","slug":"structures-of-pointer-structures-in-structures","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=2708","title":{"rendered":"Structures of Pointer Structures in Structures"},"content":{"rendered":"<p>Nested structures present another opportunity for pointers to growl in your code. The struggle is knowing which structure member operator to use &mdash; and where.<br \/>\n<!--more--><br \/>\nThe following code shows a nested structure. The <code>firstlast<\/code> structure contains strings <code>first<\/code> and <code>last<\/code>. It&#8217;s nested inside the <code>person<\/code> structure, which is then filled with data. Because no pointers lurk within the code, the standard structure member operator (<code>.<\/code>) is 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 firstlast { char first[24]; char last[24]; };\r\n    struct person { struct firstlast name; int age; };\r\n    struct firstlast washington;\r\n    struct person president;\r\n\r\n<span class=\"comments\">\/* fill the structure *\/<\/span>\r\n    president.name = washington;\r\n    strcpy(president.name.first,\"George\");\r\n    strcpy(president.name.last,\"Washington\");\r\n    president.age = 57;\r\n\r\n<span class=\"comments\">\/* display *\/<\/span>\r\n    printf(\"President %s %s was %d years old.\\n\",\r\n        president.name.first,\r\n        president.name.last,\r\n        president.age);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>You may see a warning upon compile because structure <code>washington<\/code> is used in Line 12 before its members are assigned values. That&#8217;s not a problem in this code.<\/p>\n<p>Here&#8217;s the output:<\/p>\n<pre><code>President George Washington was 57 years old.<\/code><\/pre>\n<p>In the following variation, the structure members <code>first<\/code>, <code>last<\/code>, and <code>age<\/code> are all pointers:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;stdlib.h&gt;\r\n#include &lt;string.h&gt;\r\n\r\nint main()\r\n{\r\n    struct firstlast { char *first; char *last; };\r\n    struct person { struct firstlast name; int *age; };\r\n    struct firstlast washington;\r\n    struct person president;\r\n\r\n<span class=\"comments\">\/* allocate storage *\/<\/span>\r\n    washington.first = (char *)malloc(24 * sizeof(char));\r\n    washington.last = (char *)malloc(24 * sizeof(char));\r\n    president.age = (int *)malloc(1 * sizeof(int));\r\n    if( washington.first==NULL\r\n        || washington.last==NULL\r\n        || president.age == NULL )\r\n    {\r\n        puts(\"Unable to allocate memory\");\r\n        exit(1);\r\n    }\r\n\r\n<span class=\"comments\">\/* fill the structure *\/<\/span>\r\n    president.name = washington;\r\n    strcpy(president.name.first,\"George\");\r\n    strcpy(president.name.last,\"Washington\");\r\n    *president.age = 57;\r\n\r\n<span class=\"comments\">\/* display *\/<\/span>\r\n    printf(\"President %s %s was %d years old.\\n\",\r\n        president.name.first,\r\n        president.name.last,\r\n        *president.age);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The bulk of the code, Lines 12 through 22, deals with allocating space for the three pointers. Yet, the member operator is always the dot. That&#8217;s because the structure variables themselves aren&#8217;t pointers.<\/p>\n<p>By the way, the variable <code>president.age<\/code> is a memory location and the variable <code>*president.age<\/code> is the value at that location. This type of pointer notation is required, even within a structure. You see that usage in Lines 28 and 34.<\/p>\n<p>Regardless of what happens with the variables, when the structure itself is a pointer, you must use the structure pointer member operator:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;stdlib.h&gt;\r\n#include &lt;string.h&gt;\r\n\r\nint main()\r\n{\r\n    struct firstlast { char first[24]; char last[24]; };\r\n    struct person { struct firstlast *name; int age; };\r\n    struct firstlast *washington;\r\n    struct person *president;\r\n\r\n<span class=\"comments\">\/* allocate storage *\/<\/span>\r\n    washington = (struct firstlast *)malloc(1 * sizeof(struct firstlast));\r\n    president = (struct person *)malloc(1 * sizeof(struct person));\r\n    if( washington==NULL || president==NULL )\r\n    {\r\n        puts(\"Unable to allocate memory\");\r\n        exit(1);\r\n    }\r\n\r\n<span class=\"comments\">\/* fill the structure *\/<\/span>\r\n    president-&gt;name = washington;\r\n    strcpy(president-&gt;name-&gt;first,\"George\");\r\n    strcpy(president-&gt;name-&gt;last,\"Washington\");\r\n    president->age = 57;\r\n\r\n<span class=\"comments\">\/* Display *\/<\/span>\r\n    printf(\"President %s %s was %d years old.\\n\",\r\n        president-&gt;name-&gt;first,\r\n        president-&gt;name-&gt;last,\r\n        president-&gt;age);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>Both structures <code>washington<\/code> and <code>president<\/code> are pointers. Therefore, the -&gt; operator is used to access any of their members, as shown above.<\/p>\n<p>Because <code>washington<\/code> is a pointer, the <code>name<\/code> member in structure <code>person<\/code> is also declared as a pointer, as shown in Line 8.<\/p>\n<p>If you were to use pointer members, as shown in the second example, then you&#8217;d have quite a few additional <em>malloc()<\/em> statements, but the member notation changes only when the structure itself is a pointer.<\/p>\n<p>If you do use pointer members, remember that you must allocate storage for the structure first, confirm that it&#8217;s available, and then allocate storage for the members. Also, you must use the following format when referencing the pointer <code>age<\/code> inside pointer variable <code>president<\/code>:<\/p>\n<p><code>*president->age<\/code><\/p>\n<p>To allocate the variable&#8217;s storage, you omit the <code>*<\/code> operator, but you must still use <code>president-&gt;age<\/code> when structure <code>president<\/code> is a pointer.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>How far down the rabbit hole can you go when it comes to using the <code>-&gt;<\/code> or <code>.<\/code> operator in a structure. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=2708\">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-2708","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\/2708","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=2708"}],"version-history":[{"count":5,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2708\/revisions"}],"predecessor-version":[{"id":2718,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2708\/revisions\/2718"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2708"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2708"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2708"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}