Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It seems most of your questions were answered, but for the sake of completeness, let's review.</p> <ol> <li>The reason why nothing is printing where you expect is because you've initialized the array to contain all <code>0</code> values. A <code>0</code> is a null-terminator for c-strings, meaning it marks the end of a string. It is a non-printing character as well, we don't want to end every c-string with a printing-character, that wouldn't make sense. If you want the ASCII character 0, you would need <code>'0'</code> which is decimal <code>48</code>. (See: <a href="http://www.asciitable.com/" rel="nofollow">http://www.asciitable.com/</a>)</li> <li>When declaring and initializing an array with data, it will fill each element with the corresponding data in your initialization list. In example; <code>char myArray[10] = {'a', 'b', 'c'};</code> would initialize the array with the values <code>myArray[0] = 'a'; myArray[1] = 'b'; myArray[2] = 'c';</code> but what about all of the other elements? It doesn't assume you want one element repeated, and it also assumes you don't want garbage. Instead it fills the array up with <code>0</code> values.</li> <li><p>I assume your next question might be along the lines of "How can I fill it with a different value?" There is no way to change how the array is initialized without explicitly stating each and every value for each and every element. <em>But</em> - you can set each value.</p> <pre><code>unsigned int i; char myArray[100]; // Don't waste time initializing any data yet. for(i = 0; i &lt; 100; ++i) { myArray[i] = 'a'; // Set each element to 'a' - Now it's initialized! } </code></pre> <p>Another option is to use <code>memset();</code>, which is a part of <code>&lt;string.h&gt;</code>. This will set each element to a given value, and for your case is probably what you need. This is done like so:</p> <pre><code>#include &lt;string.h&gt; char myArray[100]; // Declare, don't initialize. memset(myArray, 'a', 100); // Initialize by setting each value to 'a'! </code></pre></li> </ol> <p>Hope this helped! It may be useful to just practice and play around with c-strings to understand them a bit better. Just a note, there is a difference between <code>'0'</code> and <code>0</code>, <code>0</code> is just the value of zero, whereas <code>'0'</code> is the character value for the zero character. (Read: <code>48</code>). So if you want to memset to print 0, you have to <code>memset(myArray, '0', 100);</code></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload