{"id":3708,"date":"2019-08-17T00:01:57","date_gmt":"2019-08-17T07:01:57","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=3708"},"modified":"2019-08-24T10:09:50","modified_gmt":"2019-08-24T17:09:50","slug":"structures-and-arrays-part-i","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=3708","title":{"rendered":"Structures and Arrays, Part I"},"content":{"rendered":"<p>Structures in C have their own issues when it comes to structure-pointers and structures within pointers. I <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=2695\">wrote about this issue<\/a> a few months back.<\/p>\n<p>Another structure issue occurs with arrays within structures and arrays of structures. It also involves interesting notation \u2014 especially when you add pointers to the mix.<br \/>\n<!--more--><br \/>\nLike any variable, you can stock a structure with one or more arrays:<\/p>\n<p><code>struct player {<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;char name[32];<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;int scores[10];<br \/>\n};<\/code><\/p>\n<p>The arrays are accessed like any member in the structure: use the dot notation or reference a member or use the pointer <code>-&gt;<\/code> notation when the structure is a pointer.<\/p>\n<p>For example, if <code>one<\/code> were a <em>player<\/em> structure, you use <code>one.scores[1]<\/code> to access the second element in the <em>scores<\/em> array. If <code>one<\/code> were a pointer to a <em>player<\/em> structure, you use <code>one-&gt;scores[1]<\/code>. But first things first.<\/p>\n<p>The following code allocates <code>one<\/code> as a <em>player<\/em> structure, fills and outputs the values:<\/p>\n<pre class=\"screen\">#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\">\/* assign player name *\/<\/span>\r\n    strcpy(one.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.scores[x] = rand() % 100;\r\n\r\n    <span class=\"comments\">\/* output results *\/<\/span>\r\n    printf(\"%s's scores:\\n\",one.name);\r\n    for( x=0; x&lt;10; x++)\r\n        printf(\" %3d\",one.scores[x]);\r\n    putchar('\\n');\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The <em>player<\/em> structure is defined starting at Line 8 and variable <code>one<\/code> is declared at Line 11.<\/p>\n<p>The structure&#8217;s <em>name<\/em> member is filled at Line 15. The <em>scores<\/em> member is filled with random values at Lines 19 and 20.<\/p>\n<p>Because member <em>name<\/em> is a <em>char<\/em> array (string), the <em>strcpy()<\/em> function accesses it directly, <code>one.name<\/code>.<\/p>\n<p>Each of the <code>int<\/code> values stored in the <em>scores<\/em> array are accessed with structure dot and array bracket notation: <code>one.scores[x]<\/code>.<\/p>\n<p>Results are output from Lines 23 through 26:<\/p>\n<p><code>Billy Zlotnick's scores:<br \/>\n75\u00a0\u00a0\u00a02\u00a0\u00a034\u00a0\u00a079\u00a0\u00a057\u00a0\u00a060\u00a0\u00a033\u00a0\u00a057\u00a0\u00a087\u00a0\u00a086<\/code><\/p>\n<p>To add a level of complexity, suppose the player structure were itself an array:<\/p>\n<p><code>struct player {<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;char name[32];<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;int scores[10];<br \/>\n} p[3];<\/code><\/p>\n<p>Above, three structures of the <em>player<\/em> type are declared. The following update to the code fills each element with data:<\/p>\n<pre class=\"screen\">#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\">\/* assign player names *\/<\/span>\r\n    strcpy(p[0].name,\"Billy Zlotnick\");\r\n    strcpy(p[1].name,\"Franny Blortz\");\r\n    strcpy(p[2].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].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].name);\r\n        for( x=0; x&lt;10; x++)\r\n            printf(\" %3d\",p[y].scores[x]);\r\n        putchar('\\n');\r\n    }\r\n\r\n    return(0);\r\n}<\/pre>\n<p>Each of the <em>name<\/em> members are assigned strings at Lines 15 through 17. The format <code>p[<em>n<\/em>].name<\/code> is used, where the brackets represent structures within the array.<\/p>\n<p>Where the notation gets interesting is at Lines 24 and 32: The format <code>p[y].scores[x]<\/code> references a structure within the array and an element of the <em>scores<\/em> member. It looks complex, but if you reduce each item individually, the notation is familiar: <code>p[y]<\/code> is one of the structure array elements and <code>name[x]<\/code> is an element within the given structure.<\/p>\n<p>Here&#8217;s sample output:<\/p>\n<p><code><\/code><\/p>\n<pre>Billy Zlotnick's scores:\r\n  34  32  93  52  65  19  65  71  66  98\r\nFranny Blortz's scores:\r\n  80  18  67  17  13  14  15  82  95  55\r\nOscar Papadapolous's scores:\r\n  22   4  66  12  40  51  17  32  92  25<\/pre>\n<p>&nbsp;<\/p>\n<p>In <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3715\">next week&#8217;s Lesson<\/a>, I toss pointers into the mix.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The only time C variable notation gets heavy is when you mix structures and arrays. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3708\">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-3708","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\/3708","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=3708"}],"version-history":[{"count":10,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3708\/revisions"}],"predecessor-version":[{"id":3737,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3708\/revisions\/3737"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3708"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3708"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3708"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}