Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Very good try. There's just one tiny thing wrong, which is this:</p> <pre><code>p &lt;= 3 </code></pre> <p>Pointers are just some number which represents a memory address. When you do <code>p = alph</code>, you're not setting <code>p</code> to <code>0</code>, you're setting it to point to the same address as <code>alph</code>. When looping over an array with pointers, you have to compare the current pointer with a pointer that is one past the end of the array. To get a pointer to one element past the end of the array, you add the number of elements to the array:</p> <pre><code>alph + 3 // is a pointer to one past the end of the array </code></pre> <p>Then your loop becomes</p> <pre><code>for (; p &lt; alph + 3; ++p) </code></pre> <p>You may think that getting a pointer to one past the end of the array is going out of bounds of the array. However, you're free to get a pointer to <em>anywhere</em> in memory, as long as you don't dereference it. Since the pointer <code>alph + 3</code> is never <em>dereferenced</em> - you only use it as a marker for the end of the array - and everything is fine.</p> <p>Here are some rough correlations for the different versions:</p> <pre><code>/-----------------------------------\ | Pointer Version | Index Version | ------------------------------------- | p | i | | p = alph | i = 0 | | *p | alph[i] | | alph + 3 | 3 | | p &lt; alph + 3 | i &lt; 3 | \-----------------------------------/ </code></pre> <p>Also note that instead of doing <code>alph + 3</code>, you may want to use <code>sizeof</code>. <code>sizeof</code> gives you the number of bytes that an object occupies in memory. For arrays, it gives you the number of bytes the whole array takes up (but it doesn't work with pointers, so you can do it with <code>alph</code> but not with <code>p</code>, for example). The advantage of using <code>sizeof</code> to get the size of the array is that if you change the size later, you do not have to go and find all the places where you wrote <code>alph + 3</code> and change them. You can do that like this:</p> <pre><code>for (; p &lt; alph + sizeof(alph); ++p) </code></pre> <p>Additional note: because the size of <code>char</code> is defined to be <code>1</code> byte, this works. If you change the array to an array of <code>int</code>, for example (or any other type that is bigger than 1 byte) it will not work any more. To remedy this, you divide the total size in bytes of the array with the size of a single element:</p> <pre><code>for (; p &lt; alph + sizeof(alph) / sizeof(*alph); ++p) </code></pre> <p>This may be a little complicated to understand, but all you're doing is taking the total number of bytes of the array and dividing it by the size of a single element to find <em>the number of elements in the array</em>. Note that you are adding <em>the number of elements in the array</em>, <strong>not</strong> <em>the size in bytes of the array</em>. This is a consequence of how pointer arithmetic works (C++ automatically multiplies the number you add to a pointer by the size of the type that the pointer points to).</p> <p>For example, if you have</p> <pre><code>int alph[3]; </code></pre> <p>Then <code>sizeof(alph) == 12</code> because each <code>int</code> is 4 bytes big. <code>sizeof(*alph) == 4</code> because <code>*alph</code> is the first element of the array. Then <code>sizeof(alph) / sizeof(alph)</code> is equal to <code>12 / 4</code> which is <code>3</code>, which is the number of elements in the array. Then by doing</p> <pre><code>for (; p &lt; alph + sizeof(alph) / sizeof(*alph); ++p) </code></pre> <p>that is equivalent to</p> <pre><code>for (; p &lt; alph + 12 / 4; ++p) </code></pre> <p>which is the same as</p> <pre><code>for (; p &lt; alph + 3; ++p) </code></pre> <p>Which is correct.</p> <p>This has the good advantage that if you change the array size to <code>50</code> and change the type to <code>short</code> (or any other combination of type and array size), the code will still work correctly.</p> <p>When you get more advanced (and hopefully understand arrays enough to stop using them...) then you will learn how to use <code>std::end</code> to do all this work for you:</p> <pre><code>for (; p &lt; std::end(alph); ++p) </code></pre> <p>Or you can just use the range-based <code>for</code> loop introduced in C++11:</p> <pre><code>for (char c : alph) </code></pre> <p>Which is even easier. I recommend understanding pointers and pointer arithmetic well before reclining on the convenient tools of the Standard Library though.</p> <p>Also good job SO, you properly syntax-highlighted my ASCII-Art-Chart.</p>
    singulars
    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.
    3. VO
      singulars
      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