{"id":2695,"date":"2017-09-16T00:01:42","date_gmt":"2017-09-16T07:01:42","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=2695"},"modified":"2017-09-23T07:50:44","modified_gmt":"2017-09-23T14:50:44","slug":"pointers-on-pointers-in-structures","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=2695","title":{"rendered":"Pointers on Pointers in Structures"},"content":{"rendered":"<p>Structures are the complex variable type in C, allowing you to craft data-rich units for storing and retrieving all sorts of interesting information. They&#8217;re fun! But like asterisks falling as snowflakes, sprinkle some pointers into a structure and you can get into trouble fast.<br \/>\n<!--more--><br \/>\nIt comes down to a battle between the <code>.<\/code> and <code>-&gt;<\/code> operators, both of which reference structure members. The <code>.<\/code> operator is the one that you&#8217;re glad to use. The <code>-&gt;<\/code> implies that a pointer lurks somewhere in the structure. But where?<\/p>\n<p>This code employs a structure variable to store a person&#8217;s name. It contains two members, <em>char<\/em> arrays <code>first<\/code> and <code>last<\/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 name {\r\n        char first[24];\r\n        char last[24];\r\n    };\r\n    struct name me;\r\n\r\n    strcpy(me.first,\"Dan\");\r\n    strcpy(me.last,\"Gookin\");\r\n\r\n    printf(\"Hello, %s %s!\\n\",\r\n            me.first,\r\n            me.last);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>Lines 12 and 13 copy constant strings into the array; both use the <code>.<\/code> member operator. Here&#8217;s the output:<\/p>\n<pre><code>Hello, Dan Gookin!<\/code><\/pre>\n<p>Now hide behind your chair. For a pointer version of the code, you have several options: pointer structure, pointer members, or both pointer structure and pointer members.<\/p>\n<p>The first option is to make the structure itself a pointer:<\/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 name {\r\n        char first[24];\r\n        char last[24];\r\n    };\r\n    struct name *me;\r\n\r\n    me = (struct name *)malloc(1 * sizeof(struct name));\r\n    if( me==NULL )\r\n    {\r\n        puts(\"Unable to allocate memory\");\r\n        return(1);\r\n    }\r\n\r\n    strcpy(me-&gt;first,\"Dan\");\r\n    strcpy(me-&gt;last,\"Gookin\");\r\n\r\n    printf(\"Hello, %s %s!\\n\",\r\n            me-&gt;first,\r\n            me-&gt;last);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The <code>name<\/code> structure pointer <code>me<\/code> is declared at Line 11. It&#8217;s just a pointer, a storage container for a memory location.<\/p>\n<p>At Line 13, the <em>malloc()<\/em> function sets aside space for the <code>name<\/code> structure and saves that location in <code>me<\/code>. Because <code>me<\/code> is a pointer, the <code>-&gt;<\/code> operator must be used to address its members. You see that operator in Lines 20, 21, 24, and 25.<\/p>\n<p>The program&#8217;s output is the same as the first example.<\/p>\n<p>Now consider this code, where the members are pointers, but the structure isn&#8217;t:<\/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 name {\r\n        char *first;\r\n        char *last;\r\n    };\r\n    struct name me;\r\n\r\n    me.first = (char *)malloc( sizeof(char) * 24 );\r\n    me.last = (char *)malloc( sizeof(char) * 24 );\r\n    if( me.first==NULL || me.last==NULL)\r\n    {\r\n        puts(\"Unable to allocate memory\");\r\n        return(1);\r\n    }\r\n\r\n    strcpy(me.first,\"Dan\");\r\n    strcpy(me.last,\"Gookin\");\r\n\r\n    printf(\"Hello, %s %s!\\n\",\r\n            me.first,\r\n            me.last);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The <em>malloc()<\/em> statements at line 13 and 14 assigns storage to the <code>first<\/code> and <code>last<\/code> pointers. Because the structure itself isn&#8217;t a pointer, the standard member operator (<code>.<\/code>) is used.<\/p>\n<p>Finally, here&#8217;s a beast:<\/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 name {\r\n        char *first;\r\n        char *last;\r\n    };\r\n    struct name *me;\r\n\r\n    me = (struct name *)malloc( sizeof(struct name) * 1);\r\n    if( me==NULL )\r\n    {\r\n        puts(\"Unable to allocate memory\");\r\n        return(1);\r\n    }\r\n    me->first = (char *)malloc( sizeof(char) * 24 );\r\n    me->last = (char *)malloc( sizeof(char) * 24 );\r\n    if( me->first==NULL || me->last==NULL)\r\n    {\r\n        puts(\"Unable to allocate memory\");\r\n        return(1);\r\n    }\r\n\r\n    strcpy(me->first,\"Dan\");\r\n    strcpy(me->last,\"Gookin\");\r\n\r\n    printf(\"Hello, %s %s!\\n\",\r\n            me->first,\r\n            me->last);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>Everything&#8217;s a pointer!<\/p>\n<p>The <code>me<\/code> structure, as well as its members, must be allocated storage. The pointer <code>me<\/code> is done first, otherwise memory can&#8217;t be allocated for its members. The <code>-&gt;<\/code> member operator is required because structure <code>me<\/code> is a pointer &mdash; and that&#8217;s the only reason. If only the members were pointers, as shown in the earlier example, you use the standard <code>.<\/code> member operator.<\/p>\n<p>I hope these examples help you to understand how pointers operate within a structure. The knots get tighter when you mix structures within structures along with pointers. That dangerous ground I cover in <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=2708\">next week&#8217;s Lesson<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>It&#8217;s madness! The multivariable features too many options for messing with pointers. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=2695\">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-2695","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\/2695","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=2695"}],"version-history":[{"count":8,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2695\/revisions"}],"predecessor-version":[{"id":2722,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2695\/revisions\/2722"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2695"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2695"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2695"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}