title: C Struct with pointer to array
body: <p>I have the following code, and I'm a bit confused as to why I'm getting a segmentation fault.</p>
  
  <pre><code>typedef struct {
    int tag;
    int valid;
  } Row;
  
  typedef struct {
    int index;
    int num_rows;
    Row **rows;
  } Set;
  
  /* STRUCT CONSTRUCTORS */
  
  // Returns a pointer to a new Sow.
  // all fields of this row are NULL
  Row* new_row() {
    Row* r = malloc(sizeof(Row));
    return r;
  }
  
  // Returns a pointer to a new Set.
  // the set's index is the given index, and it has an array of
  // rows of the given length.
  Set* new_set( int index, int num_rows, int block_size ) {
    Set* s = malloc(sizeof(Set));
    s-&gt;index = index;
    s-&gt;num_rows = num_rows;
  
    Row* rows[num_rows];
    for (int i = 0; i &lt; num_rows; i++) {
      Row* row_p = new_row();
      rows[i] = row_p;
    }
    s-&gt;rows = rows;
  
    return s;
  }
  
  /* PRINTING */
  
  void print_row( Row* row ) {
    printf("&lt;&lt;T: %d, V: %d&gt;&gt;", row-&gt;tag, row-&gt;valid);
  }
  
  void print_set( Set* set ) {
    printf("[ INDEX %d :", set-&gt;index);
  
  
    for (int i = 0; i &lt; set-&gt;num_rows; i++) {
      Row* row_p = set-&gt;rows[i];
      print_row(row_p);
    }
  
    printf(" ]\n");
  }
  
  
  int main(int argc, char const *argv[]) {
  
    Set* s = new_set(1, 4, 8);
    print_set(s);
  
  
    return 0;
  
  }
  </code></pre>
  
  <p>Basically a <code>Set</code> has a pointer to a array of <code>Row</code>s. I'd think <code>Row* row_p = set-&gt;rows[i];</code> would be the right way to get the row from a set, but I must be missing something.</p>