title: Matrix as argument to a function in C
body: <p>I am stuck at the very basics. It's not homework, I'm just presenting the problem in a simplistic way.</p>
  
  <p>I need a matrix print function, and the matrix is passed as an argument along with row and col count information. The matrix is allocated on the stack.</p>
  
  <p>In function prototype mentioned below MAT_COL is a compile time define.</p>
  
  <pre><code>void matrix_print(uint8_t (*mat)[MAT_COL], int row, int col)
  </code></pre>
  
  <p>and print matrix elements as</p>
  
  <pre><code>print mat[i_row][i_col];
  </code></pre>
  
  <p>The approach will not work if I have multiple matrix with different sizes i,e "MAT_COL" is no more available.</p>
  
  <p>One possible way out would be</p>
  
  <pre><code>void matrix_print(uint8_t *in_mat, int row, int col)
  {
      uint8_t (*mat)[col] = in_mat;          // typecast "col" is an arg
      // access them as eariler
      print mat[i_row][i_col];
  }
  </code></pre>
  
  <p>Any problems with this approach ? What is the standard solution to this problem in <strong>C</strong>.</p>