{"id":6143,"date":"2023-12-09T00:01:27","date_gmt":"2023-12-09T08:01:27","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=6143"},"modified":"2023-12-02T11:42:56","modified_gmt":"2023-12-02T19:42:56","slug":"initializing-only-part-of-an-array","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=6143","title":{"rendered":"Initializing Only Part of an Array"},"content":{"rendered":"<p>I&#8217;m continually amazed when I discover some aspect of the C programming language that I&#8217;ve not encountered before. This time, the trick is how to initialize individual array elements.<br \/>\n<!--more--><br \/>\nThe standard array declaration is a statement with the data type, array name, and a part of brackets:<\/p>\n<p><code>int numbers[6];<\/code><\/p>\n<p>The brackets can show the element count, such as six above, or they can be empty. If empty, the elements are assigned directly, as shown in this code:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2023_12_09-Lesson-a.c\" rel=\"noopener\" target=\"_blank\">2023_12_09-Lesson-a.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    int numbers[] = { 10, 20, 30, 40, 50, 60 };\r\n    int x;\r\n\r\n    for( x=0; x&lt;6; x++ )\r\n        printf(\"%d\\n\",numbers[x]);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The compiler automatically allocates array storage for the six integers shown above. You could also declare the array and assign values separately. But the new thing I learned is that you need not assign all the values when declaring an array.<\/p>\n<p>For example, say the array has storage for six elements but your code wants to initialize only the fourth and fifth elements. You assign them after declaring the array like this:<\/p>\n<p><code>int numbers[6];<\/p>\n<p>numbers[3] = 40;<br \/>\nnumbers[4] = 50;<\/code><\/p>\n<p>Or you can use the format shown in this code:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2023_12_09-Lesson-b.c\" rel=\"noopener\" target=\"_blank\">2023_12_09-Lesson-b.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    int numbers[6] = {\r\n        [3] = 40,\r\n        [4] = 50\r\n    };\r\n    int x;\r\n\r\n    for( x=0; x&lt;6; x++ )\r\n        printf(\"%d\\n\",numbers[x]);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The format above is the thing I&#8217;ve not seen before: brackets with the element number and an assignment. Only elements three and four are assigned values and just by using the brackets and the element number. The remainder of the array&#8217;s elements are uninitialized, which you can see in a sample run:<\/p>\n<p><code>0<br \/>\n0<br \/>\n0<br \/>\n40<br \/>\n50<br \/>\n0<\/code><\/p>\n<p>The uninitialized elements may show zero or whatever garbage already exists in memory. Still, this format is something I&#8217;ve not seen before:<\/p>\n<p><code>int numbers[6] = {<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;[3] = 40,<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;[4] = 50<br \/>\n};<\/code><\/p>\n<p>I&#8217;m unsure of how I would use this type of initialization, though it&#8217;s legitimate and available. Yet another weird thing about C that crops up every so often.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>You can leave an array uninitialized, initialize all its elements, or just a few. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=6143\">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-6143","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\/6143","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=6143"}],"version-history":[{"count":3,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6143\/revisions"}],"predecessor-version":[{"id":6150,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6143\/revisions\/6150"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6143"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6143"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6143"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}