{"id":958,"date":"2014-09-27T00:01:11","date_gmt":"2014-09-27T07:01:11","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=958"},"modified":"2014-09-20T08:07:35","modified_gmt":"2014-09-20T15:07:35","slug":"a-few-pointers-on-structure-pointers","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=958","title":{"rendered":"A Few Pointers on Structure Pointers"},"content":{"rendered":"<p>Structures aren&#8217;t really scary. Pointers are scary. Obviously, a good way to make structures more terrifying it so throw some pointers at them.<br \/>\n<!--more--><br \/>\nA structure whipped up from memory, referenced by its address (the pointer), is really nothing too weird. It&#8217;s not like the <code>**<\/code> pointer notation, which is what makes so many Introduction to Programming students flee that course in a blanched panic.<\/p>\n<p>The structure pointer thing starts with a structure declaration, no big deal:<\/p>\n<pre><code>struct <span style=\"color:green\">human<\/span> {\r\n    int year;\r\n    int month;\r\n    int day;\r\n    char first[32];\r\n    char last[32];\r\n};<\/code><\/pre>\n<p>But then the structure variable is declared as a pointer:<\/p>\n<pre><code>struct <span style=\"color:green\">human<\/span> <span style=\"color:purple\">*president<\/span>;<\/code><\/pre>\n<p>Memory has to be allocated for the structure, and the structure pointer references that memory chunk&#8217;s address:<\/p>\n<pre><code><span style=\"color:purple\">president<\/span> = (struct <span style=\"color:green\">human<\/span> *)malloc(sizeof(struct <span style=\"color:green\">human<\/span>)*1);<\/code><\/pre>\n<p>Once created, the pointer structure&#8217;s members are referenced by using the <code>-&gt;<\/code> operator. The dot operator could also be used, but you have to mix in pointer notation, which &#8212; believe it or not &#8212; early C programmers found obscure. (Apparently such a thing happened.) So the <code>-&gt;<\/code> operator was concocted.<\/p>\n<blockquote><p>If you want to see the original structure member pointer thing operator, refer to the bottom of page 293 in my book <em>Beginning Programming with C For Dummies<\/em>. (It&#8217;s found in Chapter 20, the section &#8220;Allocating space for a structure&#8221; in case you&#8217;re reading a foreign translation.)<\/p><\/blockquote>\n<p>Here&#8217;s your sample code:<\/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 human {\r\n        int year;\r\n        int month;\r\n        int day;\r\n        char first[32];\r\n        char last[32];\r\n    };\r\n    struct human *president;\r\n\r\n<span class=\"comments\">\/* Allocate memory for the structure *\/<\/span>\r\n    president = (struct human *)malloc(sizeof(struct human)*1);\r\n    if(president == NULL)\r\n    {\r\n        perror(\"Unable to allocate memory chunk\");\r\n        exit(1);\r\n    }\r\n\r\n<span class=\"comments\">\/* fill structure data *\/<\/span>\r\n    strcpy(president-&gt;first,\"George\");\r\n    strcpy(president-&gt;last,\"Washington\");\r\n    president-&gt;year = 1732;\r\n    president-&gt;month = 2;\r\n    president-&gt;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-&gt;first,\r\n            president-&gt;last,\r\n            president-&gt;month,\r\n            president-&gt;day,\r\n            president-&gt;year);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The structure <code>human<\/code> is defined at Line 7. The <code>human<\/code> structure pointer variable <code>president<\/code> is declared at Line 14. Memory is allocated for the pointer, setting its size appropriately at Line 17. Then the structure is filled and results displayed, all by using the <code>-&gt;<\/code> operator.<\/p>\n<p>Here&#8217;s the output:<\/p>\n<pre><code>President George Washington was born on 2\/22\/1732.<\/code><\/pre>\n<p>You can go one level deeper into the C language asylum by allocating storage for the structures two strings. So the structure&#8217;s definition would contain two pointers, like this:<\/p>\n<pre><code>struct human {\r\n    int year;\r\n    int month;\r\n    int day;\r\n    char *first;\r\n    char *last;\r\n};<\/code><\/pre>\n<p>The <code>human<\/code> structure could be a referenced by a pointer or as a direct variable. Either way, pointers within the structure work the same, as shown above.<\/p>\n<p>To make the sample code a bit more bearable, I&#8217;ve reduced the size of the structure in the following code, which is an update to the previous example:<\/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 human {\r\n        int year;\r\n        char *name;\r\n    };\r\n    struct human *president;\r\n\r\n<span class=\"comments\">\/* Allocate memory for the structure *\/<\/span>\r\n    president = (struct human *)malloc(sizeof(struct human)*1);\r\n    if(president == NULL)\r\n    {\r\n        perror(\"Unable to allocate memory chunk\");\r\n        exit(1);\r\n    }\r\n<span class=\"comments\">\/* Allocate memory for the name pointer *\/<\/span>\r\n    president-&gt;name = (char *)malloc(sizeof(char)*64);\r\n    if(president-&gt;name == NULL)\r\n    {\r\n        perror(\"Unable to allocate string\");\r\n        exit(2);\r\n    }\r\n\r\n<span class=\"comments\">\/* fill structure data *\/<\/span>\r\n    strcpy(president-&gt;name,\"George Washington\");\r\n    president-&gt;year = 1732;\r\n\r\n<span class=\"comments\">\/* display results *\/<\/span>\r\n    printf(\"President %s was born in %d.\\n\",\r\n            president-&gt;name,\r\n            president-&gt;year);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The important thing to remember is that the <code>name<\/code> pointer, a member of the <code>human<\/code> structure, is referenced by using the format <code>president->name<\/code>. Technically that&#8217;s the address of the <code>president<\/code> structure in memory, and then the address of the <code>name<\/code> buffer. You don&#8217;t need to gussy up that variable name with asterisks or ampersands.<\/p>\n<p>Here&#8217;s the program&#8217;s output:<\/p>\n<pre><code>President George Washington was born in 1732.<\/code><\/pre>\n<p>If <code>president<\/code> weren&#8217;t a pointer variable, then the reference would be <code>president.name<\/code>. Even though <code>name<\/code> is a pointer within the structure, it&#8217;s still referenced like any other structure member.<\/p>\n<p>Most of the time you see pointers to structures happens with C language function calls. For example, the <em>localtime()<\/em> function returns the address of a <code>tm<\/code> structure. Its members must be read by using the <code>-&gt;<\/code> operator. One of those members could be <code>tm_zone<\/code>, which is a pointer referencing the timezone name, a string. (I write &#8220;could be&#8221; because not every library implements the <code>tm<\/code> structure identically.)<\/p>\n<p>The <em>readdir()<\/em> function also returns a structure&#8217;s address; the <code>dirent<\/code> structure contains members describing a directory (folder) entry. I&#8217;ll cover the <code>dirent<\/code> structure in a future Lesson.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summon a structure out of thin air &mdash; or available memory. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=958\">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-958","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\/958","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=958"}],"version-history":[{"count":7,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/958\/revisions"}],"predecessor-version":[{"id":991,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/958\/revisions\/991"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=958"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=958"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=958"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}