title: C - Ways to count array length
body: <p>I need the length of an array. One way works perfectly, one gives the wrong answer, and one doesn't compile.</p>
  <p>This doesn't compile:</p>
  <pre><code>size_t len = sizeof(array) / sizeof(array[0]);    
  </code></pre>
  <p>This counts only first 4 letters:</p>
  <pre><code>size_t len = sizeof(array) / sizeof(char);
  </code></pre>
  <p>And len = 12 works.</p>
  <p>I really don't understand this so some hint is greatly appreciated.</p>
  <pre><code>const char array[] = {'t', 'h', 'i', 's', 'i', 's', 'm', 'y', 't', 'e', 'x', 't', '\0'};
  
  void count_chars(const char *array, unsigned int *counts)
  {
      size_t len = 12;
      
      for(size_t i = 0; i &lt; len; i++){
          counts[(int)array[i]]++;
      }
  }
  </code></pre>