title: Passing a multidimensional variable length array to a function
body: <p>There are tons of similar questions, but still I could not find any answer relevant for the  feature of variable length arrays in C99/C11.</p>
  
  <p>How to pass <strong>multidimensional variable length array</strong> to a function in C99/C11?</p>
  
  <p>For example:</p>
  
  <pre><code>void foo(int n, int arr[][]) // &lt;-- error here, how to fix?
  {
  }
  
  void bar(int n)
  {
      int arr[n][n];
      foo(n, arr);
  }
  </code></pre>
  
  <p>Compiler (<code>g++-4.7 -std=gnu++11</code>) says:<br>
  <code>error: declaration of ‘arr’ as multidimensional array must have bounds for all dimensions except the first</code></p>
  
  <p>If I change it to <code>int *arr[]</code>, compiler still complains:<br>
  <code>error: cannot convert ‘int (*)[(((sizetype)(((ssizetype)n) + -1)) + 1)]’ to ‘int**’ for argument ‘2’ to ‘void foo(int, int**)’</code></p>
  
  <p>Next question, how to pass it by value and how to pass it by reference? Apparently, usually you don't want the entire array to be copied when you pass it to a function.<br><br></p>
  
  <p>With constant length arrays it's simple, since, as the "constant" implies, you should know the length when you declare the function:</p>
  
  <pre><code>void foo2(int n, int arr[][10]) // &lt;-- ok
  {
  }
  
  void bar2()
  {
      int arr[10][10];
      foo2(10, arr);
  }
  </code></pre>
  
  <p>I know, passing arrays to functions like this is not a best practice, and I don't like it at all. It is probably better to do with flat pointers, or objects (like std:vector) or somehow else. But still, I'm a bit curios what is the answer here from a theoretical standpoint.</p>