title: How to return a double pointer from a function and send it as input into another function?
body: <p>I am new to programming and am trying to use two functions for matrix (2D array) operations, where the output of one function is the input for the next.
  However, I do not find a way to correctly deliver the values from one function to another. When I print the outputs of the first function in main (), they are correct, but when I input them into the 2nd function and print them, the values make no sense. I have tried it a lot of ways, but it probably fails due to my lack of understanding double pointers.
  I am thankful for any hint or advise!</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  int** td (int r_in, int c_in, int r_p, int c_p, int input[][c_in],int params[][c_p]){
      int i, j, k;
      int**y_td;
      // memory allocation
      y_td = (int*)malloc(sizeof(int*)*r_in);
      for (i=0; i &lt; r_in; i++){
          y_td[i] = (int*)malloc(sizeof(int)*c_p);
      }
  
      //
      for (i=0; i &lt; r_in; i++){
          for (j=0; j &lt; c_p; j++){
              y_td[i][j]=0; // Initialization
              for (k=0; k &lt; c_in; k++){
                  y_td[i][j]+= input[i][k]*params[k][j];
              }
          }
      }
      return y_td;
  }
  
  
  int** cnv (int r_in, int c_in, int filter, int f_size, int input[][c_in], int params[][f_size][c_in]){
      int x,i,j,k,l,m,n;
      int min_len = ((r_in &lt; f_size)? r_in:f_size);
      int max_len = ((r_in &gt; f_size)? r_in:f_size);
      int r_out = max_len - min_len + 1;//rows_out
      int kernel;
      int** y_cnv;
  
      // Print input to check if it was correctly transmitted to the function
      printf("Input CV (should be equal to TD result):\n");
      for (i=0;i&lt;r_in;i++){
          for (j=0;j&lt;c_in;j++){
              printf("%d ", input[i][j]);
          }
          printf("\n");
      }
      printf("\n\n");
  
      //memory allocation
      y_cnv = (int*)malloc(sizeof(int*)*r_out);
      for (i=0; i &lt; r_out; i++){
              y_cnv[i] = (int*)malloc(sizeof(int)*filter);
      }
      //
      for (i=0; i &lt; filter; i++){
          for (k=0; k &lt; r_out; k++){
              y_cnv [k][i]=0; //initialize
          }
          for (j = 0; j &lt; c_in; j++){
              for (n = min_len-1; n &lt; max_len; n++){
                  x = n-min_len+1;
                  for (m= 0; m &lt; r_in; m++){
                      kernel = (((n-m) &lt; min_len &amp;&amp; (n-m) &gt;= 0)? params[i][n-m][j]:0);
                      y_cnv[x][i] += input[m][j]*kernel;
                  }
              }
          }
      }
      return y_cnv;
  }
  
  int main() {
      // create test arrays
      int A [4][2]= {{1,1},{2,2},{3,3},{4,4}};
      int B [2][3]= {{1,2,3},{2,3,4}};
      int C [2][2][3]= {{{1,1,1},{2,2,2}},{{3,3,3},{4,4,4}}};
  
      int** matrix;
      int i, j;
      matrix = td(4,2,2,3,A,B);
  
      // print the result of first function, which is input in 2nd function
      printf("The TD result is:\n");
      for (i=0;i&lt;4;i++){
          for (j=0;j&lt;3;j++){
             printf("%d ",matrix[i][j]);
          }
          printf("\n");
      }
      printf("\n\n");
  
      matrix = cnv(4,3,2,2,matrix,C);
  
      return 0;
  }
  </code></pre>
  
  <p>I expect the matrix printed in main () after the first function td () to be the same as when I read it in the second function cnv () and print it there, but it is not.</p>