title: How to reliably get size of C-style array?
body: <p>How do I reliably get the size of a C-style array? The method often recommended seems to be to use <code>sizeof</code>, but it doesn't work in the <code>foo</code> function, where <code>x</code> is passed in:</p>
  
  <pre><code>#include &lt;iostream&gt;
  
  void foo(int x[]) {
    std::cerr &lt;&lt; (sizeof(x) / sizeof(int)); // 2  
  }
  
  int main(){
      int x[] = {1,2,3,4,5};
      std::cerr &lt;&lt; (sizeof(x) / sizeof(int)); // 5                              
      foo(x);
      return 0;
  }
  </code></pre>
  
  <p>Answers to <a href="https://stackoverflow.com/questions/1969588/looping-a-fixed-size-array-without-defining-its-size-in-c">this question</a> recommend <code>sizeof</code> but they don't say that it (apparently?) doesn't work if you pass the array around. So, do I have to use a sentinel instead? (I don't think the users of my <code>foo</code> function can always be trusted to put a sentinel at the end. Of course, I could use <code>std::vector</code>, but then I don't get the nice shorthand syntax <code>{1,2,3,4,5}</code>.)</p>