{"id":3715,"date":"2019-08-24T00:01:29","date_gmt":"2019-08-24T07:01:29","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=3715"},"modified":"2019-08-31T08:53:47","modified_gmt":"2019-08-31T15:53:47","slug":"structures-and-arrays-part-ii","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=3715","title":{"rendered":"Structures and Arrays, Part II"},"content":{"rendered":"<p>When it comes to structures and pointers, the structure-pointer notation is used only when the structure itself is allocated as a pointer (block of memory), not when a structure member is a pointer. Let me review from <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3708\">last week&#8217;s Lesson<\/a>:<br \/>\n<!--more--><br \/>\nConsider structure <em>one<\/em> that has a pointer member <em>name<\/em>.<\/p>\n<p>If <code>one<\/code> is a standard structure variable: <code>one.name<\/code><\/p>\n<p>If <code>one<\/code> is allocated as a pointer: <code>one-&gt;name<\/code><\/p>\n<p>That&#8217;s it. Only when the structure is allocated as a pointer do you use the <code>-&gt;<\/code> notation. This rule holds true even when members are arrays, 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[32];\r\n        int scores[10];\r\n    } *one;\r\n    int x;\r\n\r\n    <span class=\"comments\">\/* allocate structure *\/<\/span>\r\n    one = (struct player *)malloc( sizeof(struct player) * 1 );\r\n    if( one==NULL )\r\n    {\r\n        fprintf(stderr,\"Unable to allocate structure\\n\");\r\n        exit(1);\r\n    }\r\n\r\n    <span class=\"comments\">\/* assign player name *\/<\/span>\r\n    strcpy(one-&gt;name,\"Billy Zlotnick\");\r\n\r\n    <span class=\"comments\">\/* create random scores *\/<\/span>\r\n    srand( (unsigned)time(NULL) );\r\n    for( x=0; x&lt;10; x++)\r\n        one-&gt;scores[x] = rand() % 100;\r\n\r\n    <span class=\"comments\">\/* output results *\/<\/span>\r\n    printf(\"%s's scores:\\n\",one-&gt;name);\r\n    for( x=0; x&lt;10; x++)\r\n        printf(\" %3d\",one-&gt;scores[x]);\r\n    putchar('\\n');\r\n\r\n    return(0);\r\n}<\/pre>\n<p>Variable <code>one<\/code> is a pointer (memory location) referencing structure <em>player<\/em>. It&#8217;s allocated at Line 15. Structure pointer notation is used at Lines 23, 28, and 33.<\/p>\n<p>For the array notation (Lines 28 and 33), this format is used: <code>one-&gt;scores[x]<\/code>.<\/p>\n<p>In the following update to the code, three <em>player<\/em> pointer structures are assigned to array <code>p[]<\/code>. The three structures are allocated individually in a <em>for<\/em> loop starting at Line 15.<\/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[32];\r\n        int scores[10];\r\n    } *p[3];\r\n    int x,y;\r\n\r\n    <span class=\"comments\">\/* allocate structures *\/<\/span>\r\n    for( y=0; y&lt;3; y++ )\r\n    {\r\n        p[y] = (struct player *)malloc( sizeof(struct player) * 1 );\r\n        if( p[y]==NULL )\r\n        {\r\n            fprintf(stderr,\"Unable to allocate structure %d\\n\",y);\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>Because array <code>p[]<\/code> is a pointer, the <code>-&gt;<\/code> notation is used to reference its members. Brackets must be used for each individual structure element; at line Line 26 you see <code>p[0]->name<\/code> reference the <em>name<\/em> member of the first structure element.<\/p>\n<p>Also check out Lines 35 and 43, which use <code>p[y]-&gt;scores[x]<\/code> to reference both structure array elements as well as array member <em>scores<\/em>&#8217; elements.<\/p>\n<p>By the way, you can allocate memory for three <em>player<\/em> structure pointers at once, 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[32];\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\">\/* 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>At Line 5, the <em>malloc()<\/em> function allocates three <em>struct player<\/em> chunks. Because of this allocation, pointer math must be used to reference the individual structures, as shown in Lines 23, 24, 25, 32, and 40. For example, <code>(p+1)-&gt;name<\/code> accesses the <em>name<\/em> member of the second <code>p<\/code> <em>player<\/em> structure allocated.<\/p>\n<p>The array member <em>scores<\/em> maintains its format: <code>(p+y)-&gt;scores[x]<\/code>, but otherwise the operators used to reference the allocated pointer structure array thingy get crazy. I continue down this path in <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3725\">next week&#8217;s Lesson<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When structures are allocated as pointers, notation takes an interesting turn. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3715\">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-3715","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\/3715","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=3715"}],"version-history":[{"count":7,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3715\/revisions"}],"predecessor-version":[{"id":3739,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3715\/revisions\/3739"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3715"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3715"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3715"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}