title: How can I calculate the number of elements in a char array?
body: <p>I was trying to calculate the number of elements in an array, and was told that the line</p>
  
  <pre><code>int r = sizeof(array) / sizeof(array[0]) 
  </code></pre>
  
  <p>would give me the number of elements in the array. And I found the method does work, at least for int arrays. When I try this code, however, things break.</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;Windows.h&gt;
  using namespace std;
  
  int main() {
      char binaryPath[MAX_PATH];
      GetModuleFileName(NULL, binaryPath, MAX_PATH);
      cout &lt;&lt; "binaryPath: " &lt;&lt; binaryPath &lt;&lt; endl;
      cout &lt;&lt; "sizeof(binaryPath): " &lt;&lt; sizeof(binaryPath) &lt;&lt; endl;
      cout &lt;&lt; "sizeof(binaryPath[0]: " &lt;&lt; sizeof(binaryPath[0]) &lt;&lt; endl;
  
      return 0;
  }
  </code></pre>
  
  <p>When this program runs binaryPath's value is</p>
  
  <pre><code>C:\Users\Anish\workspace\CppSync\Debug\CppSync.exe
  </code></pre>
  
  <p>which seems to have a size returned by sizeof (in bytes? bits? idk, could someone explain this too?) of 260. The line </p>
  
  <pre><code>sizeof(binaryPath[0]);
  </code></pre>
  
  <p>gives a value of 1.</p>
  
  <p>Obviously then dividing 260 by one gives a result of 260, which is not the number of elements in the array (by my count it's 42 or so). Could someone explain what I'm doing wrong?</p>
  
  <p>I have a sneaking suspicion that isn't actually an array as I think of it (I come from Java and python), but I'm not sure so I'm asking you guys.</p>
  
  <p>Thanks!</p>