title: How to pass a variable size 2-D array to a C function
body: <p>This is something that is explained in multiple places on the internet, but for some reason the previously explained solutions do not seem to be working for me  I have an array of strings and I would like to pass the string array to a function that will print the array in a specific format.  The implementation is shown below, where <code>cols</code> represents the number of columns to print, and <code>length</code> represents the number of strings in the array.  The function should print the strings to column <code>col</code> and then continue printing on the next line.</p>
  <pre><code>#include &lt;stdio.h&gt;
  
  void print_string_vec(size_t cols, size_t length, char vec[][length]) {
      size_t rows;
      size_t temp = length;
      size_t ncols = cols;
      size_t index = 0;
      if (length % cols == 0) rows = length / cols;
      else rows = (length / cols) + 1;
  
      printf(&quot;[ &quot;);
  
      for (size_t i = 0; i &lt; rows; i++) {
          if (temp &lt;= cols) ncols = temp - 1;
          for (size_t j = 0; j &lt; ncols; j++) {
              printf(&quot;%s, &quot;, vec[index]);
              index++;
          }
          temp -= ncols;
          if (temp == 1) break;
          printf(&quot;\n&quot;);
          printf(&quot;  &quot;);
      }
      printf(&quot;%s &quot;, vec[length - 1]);
      printf(&quot;]\n&quot;);
  }
  
  int main() {
      char a[][10] = {{&quot;First&quot;}, {&quot;Second&quot;}, {&quot;Third&quot;}, {&quot;Fourth&quot;},
                      {&quot;Fifth&quot;}, {&quot;Sixth&quot;}, {&quot;Seventh&quot;}, {&quot;Eight&quot;},
                      {&quot;Nine&quot;}, {&quot;Tenth&quot;}};
      print_string_vec(5, 10, a);
  }
  </code></pre>
  <p>I am expecting the following output</p>
  <pre><code>[ First, Second, Third, Fourth, Fifth,
    Sixth, Seventh, Eighth, Ninth, Tenth ]
  </code></pre>
  <p>However this consistently fails in compile time with the following warning;</p>
  <pre><code>error: use of parameter outside function body before `]` token
  </code></pre>
  <p>Which indicates that it has a problem with passing a variable <code>length</code> to the muti-array instantiation.  If I replace this variable with <code>20</code> as a hard coded variable, it works fine, but then I have hard-coded a function to only work with one size array.  From what I can read this is an issue with compilers that are not C99 compliant, but mine is.   Any thoughts or suggestions would be appreciated.  I am using gcc 12.1.0 and CMake to build the executable(s).  I have specified C99 and later versions as the minimum build type and I get the same results.</p>