Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Your problem (apart from compilation issues noted in my comment to the question) is that pointers are not as small as <code>3</code>. You need:</p> <pre><code>for (p = alph; p &lt; alph + sizeof(alph); p++) </code></pre> <p>for the loop. Note that <code>sizeof()</code> generates a compile-time constant. (In C99 or C2011, that is not always the case; in C++, it is always the case, AFAIK). In this context, <code>sizeof()</code> is a fancy way of writing <code>3</code>, but if you add new characters to the array, it adjusts automatically, whereas if you write <code>3</code> and change things, you have to remember to change the <code>3</code> to the new value too.</p> <hr> <h3>Ruminations on the use of <code>sizeof()</code></h3> <p>As <a href="https://stackoverflow.com/users/596781/kerrek-sb">Kerrek SB</a> points out, <code>sizeof()</code> returns the size of an array in bytes. By definition, <code>sizeof(char) == 1</code>, so in this context, it was safe to use <code>sizeof()</code> on the array. There are also times when it is not safe - or you have to do some extra work. If you had an array of some other type, you can use:</p> <pre><code>SomeType array[] = { 2, 3, 5, 7, 11, 13 }; </code></pre> <p>then the number of elements in the array is <code>(sizeof(array)/sizeof(array[0]))</code>. That is, the number of elements is the total size of the array, in bytes, divided by the size of one element (also in bytes).</p> <p>The other big gotcha is that if you 'pass an array' as a function argument, you can't use <code>sizeof()</code> on it to get the correct size - you get the size of a pointer instead. This is a good reason for not using C-style strings or C-style arrays: use <code>std::string</code> and <code>std::vector&lt;SomeType&gt;</code> instead, not least because you can find their actual size reliably with a member function call.</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. This table or related slice is empty.
    1. 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