title: realloc() invalid pointer error when increasing size of string in a function
body: <p>When i run code it show <code>realloc()</code> invalid pointer error.</p>
  
  <p>Is anything wrong in <code>input()</code> function?</p>
  
  <pre><code>#include&lt;stdio.h&gt;
  #include&lt;string.h&gt;
  #include&lt;stdlib.h&gt;
  #include&lt;time.h&gt;
  char *input(void)
  {
      int n = 1;
      char *str = malloc(sizeof(char));
      *str = '\0';
      while((*str=getchar())!='\n')
      {
          n++;
          str = realloc(str,sizeof(char)*n);
          str++;
      }
      return str;
  }
  
  int main(int argc, char const *argv[])
  {
      char *str = input();
      printf("%s",str);
      free(str);
      return 0;
  }
  </code></pre>