title: Flexible array of pointers to char in a struct
body: <p>I'm trying to implement a ring buffer with the following struct</p>
  
  <pre><code>/*head, tail are indexes of the head and tail of ring buffer
   *count is the number of elements; size is the max size of buffer
   *rbArray is an array to pointer of char used to store strings    
   */
  struct rb{
    int head;
    int tail;
    int count;
    int size;
    char *rbArray[];
  };
  </code></pre>
  
  <p>Then I use the following function to create a string buffer:</p>
  
  <pre><code> struct rb *create(int n){
       /*allocate memory for struct*/
       struct rb *newRb = (struct rb*)malloc(sizeof(struct rb)+ n*sizeof(char *));
       assert(newRb);
  
       int i;
       for(i=0;i&lt;n;i++)
          newRb-&gt;rbArray[i] = NULL;
  
       /*put head and tail at the beginning of array
       initialize count, set max number of elements*/
       newRb-&gt;head = 0;
       newRb-&gt;tail = 0;
       newRb-&gt;count = 0;
       newRb-&gt;size = n;
  
       return newRb;
     }
  </code></pre>
  
  <p>I call this function in main:</p>
  
  <pre><code> struct rb *newRB = (struct rb*)create(100);
  </code></pre>
  
  <p>However, I have problem right at the step allocating memory for struct. In the debugging mode, I can see the value of head, tail, count, were assigned very strange large numbers but not 0. And program hangs after this very first step without throwing me any exception.</p>
  
  <p>Could someone help me explain this problem please? How can I fix it?</p>