title: How do I find the size of a matrix from a linked list?
body: <p>(I edited the programm since I was asked for a Minimum Reproducible Example)
  I have to build a dinamically linked list (specifically), each node having a value of the type float and it's location (row and column) in a sparce matrix. The matrix itself doesn't have a defined size, so I have to find it to print the matrix with the values in a format like:</p>
  <pre><code>0.0  0.0  3.0
  1.0  0.0  0.0  
  0.0  0.0  0.0  
  </code></pre>
  <p>The value is zero if it's not in the list, otherwise, I have to print it's value in the corresponding location from the &quot;node-&gt;row&quot; and &quot;node-&gt;col&quot; data from it's node.</p>
  <p>Until now, I have tryed to find the size by searching the list with two functions, one for finding the biggest row value, another for finding the biggest column value:</p>
  <pre><code>/*
  Matrix is defined as:
  typedef struct node* Matrix;
  in .h file
  */
  struct node{
      float value;
      int row, col;
      struct node *next;
  };
  
  typedef struct node node;
  
  Matrix* create_matrix(){
      Matrix* m = (Matrix*) malloc(sizeof(Matrix));
      *m = NULL;
      return m;
  }
  
  void free_matrix(Matrix* m){
      if(m != NULL){
          node *n;
          while((*m != NULL)){
              n = *m;
              *m = (*m)-&gt;next;
              free(n);
          }
          free(m);
      }
  }
  
  /*int size_r(Matrix* m){
      if(empty_matrix(m)) return 0;
      int r = 0;
      node *n = *m;
      while(n != NULL){
          if(n-&gt;row &gt; r){
              r = n-&gt;row;
          }
          n = n-&gt;next;
      }
      return r;
  }
  
  int size_c(Matrix* m){
      if(empty_matrix(m)) return 0;
      int c = 0;
      node *n = *m;
      while(n != NULL){
          if(n-&gt;col &gt;= c)
              c = n-&gt;col;
          n = n-&gt;next;
      }
      return c;
  }*/
  
  //The size functions are commented since I have been trying
  //to implement their method inside the new print_matrix
  
  int empty_matrix(Matrix *m){
      if(m == NULL) return 1;
      if(*m == NULL) return 1;
      return 0;
  }
  
  int insert_value(Matrix *m, float d, int r, int c){
      if(m == NULL) return 0;
      node *n = (node*) malloc(sizeof(node));
      if(n == NULL) return 0;
      n-&gt;value = d;
      n-&gt;row = r;
      n-&gt;col = c;
      n-&gt;next = *m;
      *m = n;
      return 1;
  }
  
  /*
  //I thought this one was working, but after testing around, it returns 1
  //even if only row or column matches the argument
  
  int verify_position(Matrix *m, int r, int c){
      if(empty_matrix(m)) return 0;
      if(r &lt; 0 || c &lt; 0) return 1;
      node *n = *m;
      while(n != NULL &amp;&amp; n-&gt;row != r &amp;&amp; n-&gt;col != c){
          n = n-&gt;next;
      }
      if(n == NULL) return 0;
      else{
          return 1;
      }
  }*/
  
  /*int print_matrix(Matrix* m){
      if(empty_matrix(m)) return 0;
          int sr = size_r(m), sc = size_c(m);
          float matrix [sl][sc];
          for(int i = 0; i &lt;= sr; i++){
              for(int j = 0; j &lt;= sc; j++){
                  node *n = *m;
                  while(n != NULL &amp;&amp; n-&gt;row != r &amp;&amp; n-&gt;col != c){
                      n = n-&gt;next;
                  }
                  if(n == NULL)
                      matrix[i][j] = 0.f;
                  else
                      matrix[i][j] = n-&gt;value;
                  
                  printf(&quot;%2.f &quot;,matrix[i][j]);
              }
              printf(&quot;\n&quot;);
          }
  
      return 1;
  }*/
  
  int print_matrix(Matrix* m){
      if(empty_matrix(m)) return 0;
      int r = 0, c = 0;
      node *n = *m;
      while(n != NULL){
          if(n-&gt;row &gt; r)
              r = n-&gt;row;
          if(n-&gt;col &gt; c)
              c = n-&gt;col;
          n = n-&gt;next;
      }
      float matrix [r][c];
      for(int i = 0; i &lt;= r; i++){
          for(int j = 0; j &lt;= c; j++){
              node *aux = *m;
              while(aux != NULL &amp;&amp; aux-&gt;row != i &amp;&amp; aux-&gt;col != j){
                  aux = aux-&gt;next;
              }
              if(aux == NULL)
                  matrix[i][j] = 0.f;
              else{
                  matrix[i][j] = aux-&gt;value;
              }
              printf(&quot;%2.f &quot;,matrix[i][j]);
          }
          printf(&quot;\n&quot;);
      }
      return 1;
  }
  
  int dump_matriz(const char *tag, const Matriz *m) {
      printf(&quot;%s (%p):\n&quot;, tag, m);
      if (m == NULL) return;
      node *n = *m;
      while (n != NULL) {
          printf(&quot;\n%p: [%d][%d] = %f\n&quot;, (void*)n, n-&gt;row, n-&gt;col, n-&gt;value);
          n = n-&gt;next;
      }
      return 1;
  }
  </code></pre>
  <p>When I try my printing function in the console, it just closes it.
  If I try using the size functions, the return value is never one of the rows or columns.
  After using the dump_matrix, suggested in a comment, it printed:</p>
  <pre><code>Result:
   (00000000006F16E0):
  
  00000000006F19A0: [1073741824][1073741824] = 1091567616.000000
  
  00000000006F1950: [1073741824][1065353216] = 1090519040.000000
  
  00000000006F1900: [1073741824][0] = 1088421888.000000
  
  00000000006F18B0: [1065353216][1073741824] = 1086324736.000000
  
  00000000006F1860: [1065353216][1065353216] = 1084227584.000000
  
  00000000006F1810: [1065353216][0] = 1082130432.000000
  
  00000000006F17C0: [0][1073741824] = 1077936128.000000
  
  00000000006F1770: [0][1065353216] = 1073741824.000000
  
  00000000006F1720: [0][0] = 1065353216.000000
  </code></pre>
  <p>for values 1 through 9 in a 3x3 matrix.</p>