title: How to pass a 2D dynamically allocated array to a function?
body: <p>I have a 2 dimensional array dynamically allocated in my C code, in my function main. I need to pass this 2D array to a function. Since the columns and rows of the array are run time variables, I know that one way to pass it is :</p>
  
  <p>-Pass the rows and column variables and the pointer to that [0][0] element of the array </p>
  
  <pre><code>myfunc(&amp;arr[0][0],rows,cols)
  </code></pre>
  
  <p>then in the called function, access it as a 'flattened out' 1D array like:</p>
  
  <pre><code>ptr[i*cols+j]
  </code></pre>
  
  <p>But I don't want to do it that way, because that would mean a lot of change in code, since earlier, the 2D array passed to this function was statically allocated with its dimensions known at compile time.</p>
  
  <p>So, how can I pass a 2D array to a function and still be able to use it as a 2D array with 2 indexes like the following?</p>
  
  <pre><code>arr[i][j].
  </code></pre>
  
  <p>Any help will be appreciated.</p>