title: C double pointer realloc error
body: <p>hello friends :) i'm practicing C programming. in this program i have a task to make array of string. i have no idea what's wrong here...probably something about realloc, error i get is _crtisvalidheappointer </p>
  
  <pre><code>#define _CRT_SECURE_NO_WARNINGS
  #define MAX 100
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  void readString(char **s)
  {
  int i = 0;
  char c;
  printf("\nInput string:     ");
  while ((c = getchar()) != '\n')
  {
      i++;
      *s = realloc(*s, i*sizeof(char*));
      if (*s == NULL) { printf("Memory allocation failed!"); exit(1); }
      (*s)[i - 1] = c;
  }
  *s = realloc(*s, (i + 1)*sizeof(char));
  if (*s == NULL) { printf("Memory allocation failed!"); exit(1); }
  (*s)[i] = '\0';
  }
  
  
  char **load_words()
  {
  int cnt=0,wordcnt=0,i=0;
  char **words = NULL, *input = NULL; 
  readString(&amp;input);
  while (input[cnt] != '\0' &amp;&amp; cnt &lt; strlen(input))
  {
      words = realloc(words, ++wordcnt);//errors in second repeat of the loop
      words[wordcnt] = malloc(MAX);
      i = 0;
      while (input[cnt] != ' ')
      {
          words[wordcnt][i++] = input[cnt++];
      }
      words[wordcnt][i] = '\0';
      realloc(words[wordcnt], (i + 1)*sizeof(char));
  }
  realloc(words, wordcnt);
  free(input);
  return words;
  }
  
  void main()
  {
   int i;
   char **words = NULL;
   words = load_words();
   scanf("%d", &amp;i);
  }
  </code></pre>
  
  <p>can someone help me and tell me what did i do wrong here? this function should return array of strings but array should be double pointer(string matrix)</p>