title: Treating 2D array as 1D array
body: <p>Let's say we have a 2D <code>int</code> array:</p>
  
  <pre><code>int a[3][4] = { { 1,3,2,4 }, { 2,1,5,3 }, { 0,8,2,3 } };
  </code></pre>
  
  <p>Is it legal and valid to take its address and reinterpret it as a pointer to 1D array of <code>int</code>s? Basically:</p>
  
  <pre><code>int *p = reinterpret_cast&lt;int *&gt;(&amp;a);
  </code></pre>
  
  <p>So that I can do things like (roughly):</p>
  
  <pre><code>template&lt;typename T, size_t X, size_t Y&gt;
  void sort2(T(&amp;arr)[X][Y])
  {
      T *p = reinterpret_cast&lt;T *&gt;(&amp;arr);
      std::sort(p, p + X*Y);
  }
  </code></pre>
  
  <p>DEMO: <a href="https://ideone.com/tlm190" rel="noreferrer">https://ideone.com/tlm190</a></p>
  
  <p>To my knowledge, the standard guarantees that alignment of 2D array would be contiguous in memory, and although <code>p + X*Y</code> technically is out of range is never accessed so should not lead to Undefined Behaviour either. </p>
  
  <p>Can I absolutely treat 2D arrays as 1D arrays when needed?</p>