title: reallocating memory doesn't work in c
body: <p>What I'm trying to do in my program is to copy the content of one string to another, in reverse. This part of the program works.</p>
  
  <p>However, I don't want to limit the user for input, so I want to use malloc and realloc. This is my code:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  /*copy one string to another, in reverse*/
  void copyStr(char *p, char *h){
  
     int i=0,j=0;
     int length=0;
     length=strlen(p); int l=length;
     for (i=0; i&lt;length; i++){
         h[i]=p[l-1];
         l--;
     }
     char *temp=&amp;h[0];
     for (i=0; i&lt;length; i++){
         printf("%c",temp[i]);
     }
  
  
  }
  main(){
      printf("please enter a string\n");
      char c; int i=0; int end=10;
      /*allocate initial memory*/
      char *p=(char*)malloc(sizeof(end)); char *temp=p;
      while (c!='\n')
      {
          /*reallocate if needed*/
          if (i==(end-1)){
              end*=2;
               temp = realloc(p,end*sizeof(temp));
              if (temp!=NULL){
                  /*this is for myself, to see what the error was*/
                  printf("error allocating\n");
                  exit(1);
              }
              else
                  free(p);
          }
          c=getchar();
          p[i]=c;
          i++;
      }
  
      char h [sizeof(p)];
      copyStr(p,h);
  }
  </code></pre>
  
  <p>I found out that the realloc function doesn't work and so I am asking for your help.</p>
  
  <p>The program works if the input is very short (i.e 3 chars). 
  If it longer than 10 letters, it will not reallocate memory.
  If it longer than 5, it will print reversly but will send me a message called "stack smashed".
  Thank you.</p>