title: Realloc, char**, segfault
body: <p>There's a function. It is add_lexem and adds an element (char *) in the end of specified array and. If no memory left, it allocates some extra memory (100 * sizeof(char *)). That function causes segfault, which is the problem.</p>
  
  <pre><code>#include &lt;string.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;stdio.h&gt;
  
  void add_lexem(char **lexems, int *lexemsc, int *lexem_n, const char *lexem)
  {
      if (*lexem_n &gt;= *lexemsc) {
          lexems = realloc(lexems, sizeof(char *) * (*lexemsc + 100));
          *lexemsc += 100;
      }
  
      char *for_adding = malloc(sizeof(char) * strlen(lexem));
      strcpy(for_adding, lexem);
      lexems[*lexem_n] = for_adding;
      (*lexem_n)++;
  }
  
  int main(void)
  {
      char **D = malloc(sizeof(char *) * 2);
      int lexemsc = 2;
      int lexem_n = 0;
  
      add_lexem(D, &amp;lexemsc, &amp;lexem_n, "MEOW");
      printf("%s\n", D[0]);
  
      add_lexem(D, &amp;lexemsc, &amp;lexem_n, "BARK");
      printf("%s\n", D[1]);
  
      // in this place lexem_n becomes equal lexemsc
      add_lexem(D, &amp;lexemsc, &amp;lexem_n, "KWARK"); 
      printf("%s\n", D[2]);
  
      return 0;
  }
  </code></pre>
  
  <p>The output must be </p>
  
  <pre><code>MEOW
  BARK
  KWARK
  </code></pre>
  
  <p>but it is</p>
  
  <pre><code>MEOW
  BARK
  Segmentation fault (core dumped)
  </code></pre>