title: Passing pointers (matrix) to a function in c
body: <p>I have dynamically created a matrix using calloc in the usual way:</p>
  
  <pre><code>int **matrix;
  int dim,r;
  scanf("%d",&amp;dim);
  matrix=(int **)calloc(dim, sizeof(int *));
  for(r=0; r&lt;dim; r++)
     {
      matrix[r]=(int *)calloc(dim, sizeof(int));
     }
  </code></pre>
  
  <p>Now if I wanted to create a function to just print the elements of this matrix, I should write something like:</p>
  
  <pre><code>void stampmatrix(int **matrix, int dim)
  {
  int r=0, c=0;
  for(r=0; r&lt;dim; r++)
      {
      printf("(");
      for(c=0;c&lt;dim;c++)
          {
          printf(" %d , ",matrix[r][c]);
          }
      printf(")");
       }
  }
  </code></pre>
  
  <p>And this works fine.
  Now I add this line to the previous function</p>
  
  <pre><code>`...`
   matrix[r][c]=1;
   printf(" %d , ",matrix[r][c]);
   ...
  </code></pre>
  
  <p>If i call this function in my main function, 
      stampmatrix(matrix,dim)
  once i run the program, the compiler should create a copy of my matrix, fill it with 1, and then print them, and then return to my main function <strong>without changing the actual matrix</strong>. But if I do this and then i check in my main function the values of my matrix elements, they are changed to 1.
  In class i was told that if I pass values to a function, the program creates a copy of the values, works with them and then cancel the copy, so I need to pass addresses to a function in order to actually change the contents of my variables in my main function.
  Why in this case it doesn't work, and changes my matrix values? It's because I still pass pointers to the function stampmatrix? I really don't understand. Shouldn't the function be something like:</p>
  
  <pre><code>void stampfunction(int dim, int ***matrix)
  </code></pre>
  
  <p>Or it's because i used a void function? 
  Thanks for the attention!</p>