title: How does memory reallocated for an array inside a struct
body: <p>In the code below, when memory is reallocated with </p>
  
  <pre><code>g-&gt;alist[u] = realloc(g-&gt;alist[u], sizeof(struct successors) +
  sizeof(int) * (g-&gt;alist[u]-&gt;len - 1)) 
  </code></pre>
  
  <p>how can numbers be added to <code>list[]</code> using </p>
  
  <pre><code>g-&gt;alist[u]-&gt;list[g-&gt;alist[u]-&gt;d++] = v
  </code></pre>
  
  <p>if it was initialized as <code>list[1]</code>? </p>
  
  <pre><code>struct graph {
      int n;
      int e;
          struct successors {
              int d;
              int len;
              char is_sorted;
              int list[1];        
           } *alist[1];
  };
  typedef struct graph *Graph;
  
  Graph graph_create(int n)
  {
  
      Graph g;
      int i;
  
      g = malloc(sizeof(struct graph) + sizeof(struct successors*) * (n - 1));
      assert(g);
      g-&gt;v = n;
      g-&gt;e = 0;
  
      for (i = 0; i &lt; n; i++)
      {
          g-&gt;alist[i] = malloc(sizeof(struct successors));
          assert(g-&gt;alist[i]);
  
          g-&gt;alist[i]-&gt;d = 0;
          g-&gt;alist[i]-&gt;len = 1;
          g-&gt;alist[i]-&gt;is_sorted = 1;
      }
  
      return g;
  }
  
  void graph_add_edge(Graph g, int u, int v)
  {
      assert(u &gt;= 0);
      assert(u &lt; g-&gt;v);
      assert(v &gt;= 0);
      assert(v &lt; g-&gt;v);
      while(g-&gt;alist[u]-&gt;d &gt;= g-&gt;alist[u]-&gt;len)  
      {
          g-&gt;alist[u]-&gt;len *= 2;
          g-&gt;alist[u] = realloc(g-&gt;alist[u], sizeof(struct successors) + 
          sizeof(int) * (g-&gt;alist[u]-&gt;len - 1));
      }
      g-&gt;alist[u]-&gt;list[g-&gt;alist[u]-&gt;d++] = v;
      g-&gt;alist[u]-&gt;is_sorted = 0;
      g-&gt;e++;
  }
  </code></pre>