{"id":3725,"date":"2019-08-31T00:01:44","date_gmt":"2019-08-31T07:01:44","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=3725"},"modified":"2019-08-17T11:50:15","modified_gmt":"2019-08-17T18:50:15","slug":"structures-and-arrays-part-iii","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=3725","title":{"rendered":"Structures and Arrays, Part III"},"content":{"rendered":"<p>I&#8217;ve wrapped up most of the oddities about structure\/pointer\/array notation in the past two Lessons (<a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3708\">Part I<\/a> and <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3715\">Part II<\/a>). All that&#8217;s left is for me to go insane and start allocating structures as pointers with all their members as pointers.<br \/>\n<!--more--><br \/>\nI&#8217;ll use the same <em>player<\/em> structure from the past two Lessons:<\/p>\n<p><code>struct player {<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;char name[32];<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;char scores[10];<br \/>\n};<\/code><\/p>\n<p>The conversion of a <em>char<\/em> array into a <em>char<\/em> pointer is easiest. For each <em>player<\/em> structure allocated, the <em>name<\/em> array must also be allocated, as shown in this code:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;stdlib.h&gt;\r\n#include &lt;time.h&gt;\r\n#include &lt;string.h&gt;\r\n\r\nint main()\r\n{\r\n    struct player {\r\n        char *name;\r\n        int scores[10];\r\n    } *p;\r\n    int x,y;\r\n\r\n    <span class=\"comments\">\/* allocate structures *\/<\/span>\r\n    p = (struct player *)malloc( sizeof(struct player) * 3 );\r\n    if( p==NULL )\r\n    {\r\n        fprintf(stderr,\"Unable to allocate structures\\n\");\r\n        exit(1);\r\n    }\r\n\r\n    <span class=\"comments\">\/* allocate storage for name member *\/<\/span>\r\n    for( x=0; x&lt;3; x++ )\r\n    {\r\n        p[x].name = (char *)malloc( sizeof(char) * 32 );\r\n        if( p[x].name==NULL )\r\n        {\r\n            fprintf(stderr,\"Unable to allocat string\\n\");\r\n            exit(1);\r\n        }\r\n    }\r\n\r\n    <span class=\"comments\">\/* assign player names *\/<\/span>\r\n    strcpy((p+0)-&gt;name,\"Billy Zlotnick\");\r\n    strcpy((p+1)-&gt;name,\"Franny Blortz\");\r\n    strcpy((p+2)-&gt;name,\"Oscar Papadapolous\");\r\n\r\n    <span class=\"comments\">\/* create random scores *\/<\/span>\r\n    srand( (unsigned)time(NULL) );\r\n    for( y=0; y&lt;3; y++)\r\n    {\r\n        for( x=0; x&lt;10; x++)\r\n            (p+y)-&gt;scores[x] = rand() % 100;\r\n    }\r\n\r\n    <span class=\"comments\">\/* output results *\/<\/span>\r\n    for( y=0; y&lt;3; y++)\r\n    {\r\n        printf(\"%s's scores:\\n\",(p+y)-&gt;name);\r\n        for( x=0; x&lt;10; x++)\r\n            printf(\" %3d\",(p+y)-&gt;scores[x]);\r\n        putchar('\\n');\r\n    }\r\n\r\n    return(0);\r\n}<\/pre>\n<p>Structure <code>p<\/code> is allocated at line 15. Room is set aside for three structures.<\/p>\n<p>For each structure allocated, a <em>name<\/em> pointer is allocated at Line 23. The format used is <code>p[x].name<\/code>. This format is valid because array notation is used; <code>p<\/code> is still a pointer, but for some reason the compiler is okay using dot notation with an array of pointers. Weird.<\/p>\n<p>At Lines 34, 35, and 36, I use pointer math to access each <code>p<\/code> structure: <code>(p+0)-&gt;name<\/code>. In this format, the <code>-&gt;<\/code> notation is required. Yet, if the statements were reformatted to use <code>p[0].name<\/code>, the dot is used. The compiler balks if you use <code>p[0]-&gt;name<\/code>.<\/p>\n<p>For the <em>scores<\/em> array at Lines 43 and 51, array notation is retained. But what happens when <em>scores<\/em> is allocated as an array? This code demonstrates:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;stdlib.h&gt;\r\n#include &lt;time.h&gt;\r\n#include &lt;string.h&gt;\r\n\r\nint main()\r\n{\r\n    struct player {\r\n        char *name;\r\n        int *scores;\r\n    } *p;\r\n    int x,y;\r\n\r\n    <span class=\"comments\">\/* allocate structures *\/<\/span>\r\n    p = (struct player *)malloc( sizeof(struct player) * 3 );\r\n    if( p==NULL )\r\n    {\r\n        fprintf(stderr,\"Unable to allocate structures\\n\");\r\n        exit(1);\r\n    }\r\n\r\n    <span class=\"comments\">\/* allocate storage for name and scores members *\/<\/span>\r\n    for( x=0; x&lt;3; x++ )\r\n    {\r\n        p[x].name = (char *)malloc( sizeof(char) * 32 );\r\n        p[x].scores = (int *)malloc( sizeof(int) * 10 );\r\n        if( p[x].name==NULL || p[x].scores==NULL )\r\n        {\r\n            fprintf(stderr,\"Unable to allocate storage\\n\");\r\n            exit(1);\r\n        }\r\n    }\r\n\r\n    <span class=\"comments\">\/* assign player names *\/<\/span>\r\n    strcpy((p+0)-&gt;name,\"Billy Zlotnick\");\r\n    strcpy((p+1)-&gt;name,\"Franny Blortz\");\r\n    strcpy((p+2)-&gt;name,\"Oscar Papadapolous\");\r\n\r\n    <span class=\"comments\">\/* create random scores *\/<\/span>\r\n    srand( (unsigned)time(NULL) );\r\n    for( y=0; y&lt;3; y++)\r\n    {\r\n        for( x=0; x&lt;10; x++)\r\n            *((p+y)-&gt;scores+x) = rand() % 100;\r\n    }\r\n\r\n    <span class=\"comments\">\/* output results *\/<\/span>\r\n    for( y=0; y&lt;3; y++)\r\n    {\r\n        printf(\"%s's scores:\\n\",(p+y)-&gt;name);\r\n        for( x=0; x&lt;10; x++)\r\n            printf(\" %3d\",*((p+y)-&gt;scores+x));\r\n        putchar('\\n');\r\n    }\r\n\r\n    return(0);\r\n}<\/pre>\n<p>Storage for the <em>scores<\/em> array is allocated in the <em>for<\/em> loop at Line 23. Line 26 uses array notation to assign space for 10 <em>int<\/em> values.<\/p>\n<p>The pointer notation required to reference each <em>int<\/em> value is shown at Lines 44 and 52: <code>*((p+y)->scores+x)<\/code> I&#8217;ll be honest and explain that coding this concoction wasn&#8217;t obvious.<\/p>\n<p>My first attempt was to use <code>(p+y)->*(scores+x)<\/code>. So the structure at address <code>(p+y)<\/code> references the value at address <code>(scores+x)<\/code>. But this construction doesn&#8217;t work. Instead, the format relies upon obtaining an address offset first, then accessing the value:<\/p>\n<p><code>*((p+y)->scores+x)<\/code><\/p>\n<p>First, address <code>(p+y)<\/code> is calculated. Then, a reference is made to address <code>scores+x<\/code>. At this point <code>(p+y)->scores+x<\/code> references the location of an <em>int<\/em> value. To pull this value, the entire concoction is wrapped in parentheses and prefixed with the <code>*<\/code> operator.<\/p>\n<p>At this point, I&#8217;ll stop the craziness. I think I covered nearly all the possibilities for weird notation within a structure. Though, if you can think of more, please let me know. I can handle it.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Structures can be pointers and all their members can be pointers as well. It&#8217;s nuts. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3725\">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-3725","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\/3725","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=3725"}],"version-history":[{"count":3,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3725\/revisions"}],"predecessor-version":[{"id":3734,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3725\/revisions\/3734"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3725"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3725"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3725"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}