title: Dynamically allocated 2D matrix transposition (memory effective)
body: <p>I want to transpose a matrix in C, with as little memory use as possible. Therefore it is not an option to simply create a copy of the matrix, fill it in, and delete the other one afterwards. I thus want to do every swap on the matrix itself, with 1 temp variable to reuse. Matrix elements are indexed with pointer arithmetic, but as far as I'm aware, these are correct. The problem now being that the function doesn't result in any changes. The matrix struct is dynamically allocated and has the folowing structure;</p>
  
  <pre><code>typedef struct {
  int rows;
  int cols;
  int** data;
      } matrix;
  </code></pre>
  
  <p>The transpose function I wrote now looks as follows;</p>
  
  <pre><code>void transpose(matrix* m){
  int i,j,temp;
  for(i=0;i&lt;m-&gt;row;i++){
      for(j=0;j&lt;m-&gt;col;j++){
          temp=*((*(m-&gt;data+i))+j);
          *((*(m-&gt;data+i))+j)=*((*(m-&gt;data+j))+i);
          *((*(m-&gt;data+j))+i)=temp;
      }
  }
  </code></pre>
  
  <p>}</p>
  
  <p>I already tried starting the function with</p>
  
  <pre><code>int* temp=(int*)malloc()sizeof(int);
  </code></pre>
  
  <p>and ending with</p>
  
  <pre><code> free(temp);
  </code></pre>
  
  <p>That gives me an error in Visual Studio about adres acess. Any tips?</p>