title: adding pointers to dynamic pointer array in a loop in c
body: <p>I have a structure that represents a row in a table:</p>
  
  <pre><code>typedef struct {
      char *a;
      char *b;
  } row;
  </code></pre>
  
  <p>and I have a function that initializes that row based on db data and returns a pointer to it</p>
  
  <pre><code>row* get_row(dbrow d) {
      row *r = malloc(sizeof(row));
      r-&gt;a = malloc(5);
      strcpy(r-&gt;a, d.a);
      r-&gt;b = malloc(5);
      strcpy(r-&gt;b, d.b);
      return r;
  }
  </code></pre>
  
  <p>and finally, I have a function that has an <code>row **rows</code> as a parameter:</p>
  
  <pre><code>void get_rows(row **rows) {
      ...
      rows = malloc(rowNumber * sizeof(row*));
      int i;
      for (i = 0; i &lt; rowNumber; i++) {
          rows[i] = get_row(dbrow);
      }
  }
  </code></pre>
  
  <p>get_row works as expected and returns a pointer to valid row struct, but gdb shows that rows[0] (and all the others) never gets a new value, that is, it always points to the same address, almost as if the <code>rows[i] = get_row(dbrow)</code> line doesn't exist.</p>