{"id":4306,"date":"2020-08-22T00:01:11","date_gmt":"2020-08-22T07:01:11","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=4306"},"modified":"2020-08-22T08:26:38","modified_gmt":"2020-08-22T15:26:38","slug":"initializing-an-array","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=4306","title":{"rendered":"Initializing an Array"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2020\/08\/0822-figure1-1.png\" alt=\"graphic\" width=\"531\" height=\"85\" class=\"alignnone size-full wp-image-4320\" srcset=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2020\/08\/0822-figure1-1.png 531w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2020\/08\/0822-figure1-1-300x48.png 300w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2020\/08\/0822-figure1-1-500x80.png 500w\" sizes=\"auto, (max-width: 531px) 100vw, 531px\" \/><br \/>\nLike all variables in C, arrays are declared uninitialized. They contain whatever junk already exists in memory. Initializing an array is therefore important. It can be done in one of three ways: as it&#8217;s declared, in a loop, or as it&#8217;s used in the code.<br \/>\n<!--more--><br \/>\nDeclaring an array involves assigning it a data type, name, and size:<\/p>\n<p><code>float market_close[5];<\/code><\/p>\n<p>Array <code>market_close<\/code> is composed of 5 <em>float<\/em> values. This declaration merely sets aside storage for the values; nothing is initialized.<\/p>\n<p><code>int scores[] = { 1075, 1050, 975, 850, 600 };<\/code><\/p>\n<p>Integer array <code>scores<\/code> contains 5 elements, defined as the array is declared. The number 5 isn&#8217;t required in the brackets because the compiler is smart enough to figure out that 5 elements are declared.<\/p>\n<p><code>int scores[5] = { 0 };<\/code><\/p>\n<p>In this format, integer array <code>scores<\/code> contains five elements, all zero. The <code>{ 0 }<\/code> initialization applies to all five elements. The following code shows how this trick works for a floating-point array.<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2020_08_22-Lesson-a.c\" rel=\"noopener noreferrer\" target=\"_blank\">2020_08_22-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    const int size = 10;\r\n    float v[size] = { 0.0 };\r\n\r\n    puts(\"Array Dump\");\r\n    for(int x = 0; x&lt;size; x++ )\r\n        printf(\"%2d: %f\\n\",x+1, v[x] );\r\n\r\n    return(0);\r\n}<\/pre>\n<p>Array <code>v<\/code> is declared at Line 6 and initialized to <code>size<\/code> (10) <em>float<\/em> values. The dump at Lines 9 and 10 outputs the array&#8217;s values:<\/p>\n<p><code>Array Dump<br \/>\n&nbsp;1: 0.000000<br \/>\n&nbsp;2: 0.000000<br \/>\n&nbsp;3: 0.000000<br \/>\n&nbsp;4: 0.000000<br \/>\n&nbsp;5: 0.000000<br \/>\n&nbsp;6: 0.000000<br \/>\n&nbsp;7: 0.000000<br \/>\n&nbsp;8: 0.000000<br \/>\n&nbsp;9: 0.000000<br \/>\n10: 0.000000<\/code><\/p>\n<p>This initialization trick works only for zero. if you initialize the array at Line 6 with this statement:<\/p>\n<p><code>float v[size] = { 1.0 };<\/code><\/p>\n<p>The output looks like this:<\/p>\n<p><code>Array Dump<br \/>\n&nbsp;1: 1.000000<br \/>\n&nbsp;2: 0.000000<br \/>\n&nbsp;3: 0.000000<br \/>\n&nbsp;4: 0.000000<br \/>\n&nbsp;5: 0.000000<br \/>\n&nbsp;6: 0.000000<br \/>\n&nbsp;7: 0.000000<br \/>\n&nbsp;8: 0.000000<br \/>\n&nbsp;9: 0.000000<br \/>\n10: 0.000000<\/code><\/p>\n<p>The first (zeroth) element is 1.0, but the rest are zero. I really wish all the elements were initialized to the same value, but that&#8217;s not how it works.<\/p>\n<p>If it&#8217;s your desire to initialize an array to a specific value, run a loop that sets all the array&#8217;s values. This is the approach I use in my code:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2020_08_22-Lesson-b.c\" rel=\"noopener noreferrer\" target=\"_blank\">2020_08_22-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    const int size = 10;\r\n    int v[size];\r\n\r\n    <span class=\"comments\">\/* initialize array *\/<\/span>\r\n    for(int x = 0; x&lt;size; x++ )\r\n        v[x] = -1;\r\n\r\n    <span class=\"comments\">\/* output array *\/<\/span>\r\n    puts(\"Array Dump\");\r\n    for(int x = 0; x&lt;size; x++ )\r\n        printf(\"%2d: %d\\n\",x+1, v[x] );\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The <em>for<\/em> loop at Line 9 initializes all values in the array to -1. That&#8217;s it. Output:<\/p>\n<p><code>Array Dump<br \/>\n&nbsp;1: -1<br \/>\n&nbsp;2: -1<br \/>\n&nbsp;3: -1<br \/>\n&nbsp;4: -1<br \/>\n&nbsp;5: -1<br \/>\n&nbsp;6: -1<br \/>\n&nbsp;7: -1<br \/>\n&nbsp;8: -1<br \/>\n&nbsp;9: -1<br \/>\n10: -1<\/code><\/p>\n<p>I&#8217;ve read online that the <em>gcc<\/em> compiler offered a non-standard initialization alternative. It uses an ellipses in the square brackets to assign a set value to a range of elements. The format looks something like this:<\/p>\n<p><code>float v[10] = { [1 . . . 10] 1.0 };<\/code><\/p>\n<p>The brackets in the assignment specify a range from 1 to 10 elements (or it should be 0 to 9?). These elements are given the value 1.0. On my systems, the <em>gcc<\/em> compiler doesn&#8217;t support this trick. Even so, if it works, it&#8217;s non-standard and I wouldn&#8217;t recommend it.<\/p>\n<p>The <code>= { 0 }<\/code> initialization format is available for array initialization, but I still prefer to use a loop to set clean all the elements&#8217; values. It&#8217;s not that I don&#8217;t trust the compiler but&nbsp;.&nbsp;.&nbsp;.&nbsp;No, wait. I don&#8217;t trust the compiler. Yeah, just initialize your arrays. It&#8217;s a good thing.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>It&#8217;s a basic operation because, unless you don&#8217;t initialize the array, it contains garbage. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=4306\">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-4306","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\/4306","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=4306"}],"version-history":[{"count":7,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4306\/revisions"}],"predecessor-version":[{"id":4327,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4306\/revisions\/4327"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4306"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4306"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4306"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}