title: Allocating dynamic array of struct on C.
body: <p>Allocating dynamic array of struct on C. Valgrind found this error: Use of uninitialised value of size 8. The error pops up while trying to access the struct member.</p>
  
  <p>What is the way to avoid that?</p>
  
  <pre><code>void find_rate()
  {
    int num_lines = 0;
    FILE * in;
    struct record ** data_array;
    double * distance;
    struct record user_record;
  
    in = open_file();
  
    num_lines = count_lines(in);
  
    allocate_struct_array(data_array, num_lines);
  
    data_array[0]-&gt;community_name[0] = 'h';       // the error is here
    printf("%c\n", data_array[0]-&gt;community_name[0]);
  
    fclose(in);
  }
  
  FILE * open_file()
  {
    ..... some code to open file
    return f;
  }
  
  int count_lines(FILE * f)
  {
    .... counting lines in file
    return lines;
  }
  </code></pre>
  
  <p>Here is the way I allocate the array:</p>
  
  <pre><code>void allocate_struct_array(struct record ** array, int length)
  {
    int i;
  
    array = malloc(length * sizeof(struct record *));
  
    if (!array)
      {
        fprintf(stderr, "Could not allocate the array of struct record *\n");
        exit(1);
      }
  
    for (i = 0; i &lt; length; i++)
      {
        array[i] = malloc( sizeof(struct record) );
  
        if (!array[i])
      {
        fprintf(stderr, "Could not allocate array[%d]\n", i);
        exit(1);
      }
      }
  }
  </code></pre>