title: Can I swap two matrixes with a function just interchanging its base address, without swapping element by element?
body: <p><strong>Summary</strong></p>
  
  <p>It is just a doubt that came when studying about pointers, can I swap two matrixes using a function without needing to swap element by element in it?</p>
  
  <p>I've created 2 matrixes dynamically, using malloc, and declared its elements` values in main(). So, I could swap like this:</p>
  
  <p>(Swapping element by element)</p>
  
  <pre><code>void swapMatrix(int** a, int** b){
      for(int i = 0; i&lt; row; i++){
          for(int j = 0; j&lt; col; j++){
              int tmp = a[i][j];
              a[i][j] = b[i][j];
              b[i][j] = tmp;
          }
      }
  }
  ...
  int main() {
  ...
  print(M1);
  swapMatrix(M1, M2);
  print(M1);
  ...
  </code></pre>
  
  <p>(Swapping row by row)</p>
  
  <pre><code>void swapMatrix(int** a, int** b){
      int* tmp = *a;
      *a = *b;
      *b = tmp;
      for (int i = 0; i&lt;col; i++){
          int* tmp = *(a+i);
          *(a+i) = *(b+i);
          *(b+i) = tmp;
      }
  }
  ...
  int main() {
  ...
  print(M1);
  swapMatrix(M1, M2);
  print(M1);
  ...
  </code></pre>
  
  <p><strong>The Attempts</strong></p>
  
  <p>But is there a way to just change the matrix pointer to pointer address? I've attempted the following codes:</p>
  
  <p><strong>I)</strong></p>
  
  <pre><code>void swapMatrix(int** a, int** b){
      int** tmp = a;
      a = b;
      b = tmp;
  }
  ...
  int main() {
  ...
  print(M1);
  swapMatrix(M1, M2);
  print(M1);
  ...
  </code></pre>
  
  <p>It compiled but when I verified its elements, none of them really swapped</p>
  
  <p><strong>II)</strong></p>
  
  <pre><code>void swapMatrix(int*** a, int*** b){
      int*** tmp = a;
      a = b;
      b = tmp;
  
  ...
  int main() {
  ...
  print(M1);
  swapMatrix(&amp;M1, &amp;M2);
  print(M1);
  ...
  </code></pre>
  
  <p>Again, it compiled but when I verified its elements, none of them really swapped.</p>
  
  <p>How should I implement the function in order to swap the entire matrix just changing a pointer to pointer address? Is it possible? Actually, I don't really understand why the I) attempt didn't work. I would really appreciate if you could explain the details.</p>