title: How to realloc another size with realloc
body: <p>I have a program which takes as input a Name and if the input is less then the allowed size  it will be saved in a Pointer.</p>
  
  <p>If the input is bigger then allowed size then the <code>realloc</code> function will be used to satisfy the memory needed.
  The program at this point allocates only 6 bytes, 5 for name <code>MICHI</code> and 1 for '\0'.
  If the user types <code>MICHAEL</code> then the program allocates enough memory for that pointer to fit <code>MICHAEL</code> inside that pointer.</p>
  
  <p>Here is the Program:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  
  struct person{
      char *name;
  }pers;
  
  void addMem(void){
      unsigned int length = 6;
      size_t newLength = 0;
      unsigned int newSize = 0;
      unsigned int i =0;
      char *name;
      int c;
  
      name = malloc(lenght);
      if(name == NULL){
          exit(1);
      }
      newSize = length;
  
      printf("Enter your name:&gt;  ");
  
      while ((c = getchar()) != '\n' &amp;&amp; c!=EOF){
          name[i++]=(char)c;
  
          if(i == newSize){
              newSize = i+length;
              name = realloc(name, newSize);
          }
      }
  
      name[i] = '\0';
  
      newLength = strlen(name)+1;
      pers.name = malloc(newLength);
  
      memcpy(pers.name, name, newLength);
  
      free(name);
      name = NULL;
  }
  
  int main(void){
      addMem();
  
      printf("Your name is:&gt;  %s\n",pers.name);
      free(pers.name);
      pers.name = NULL;
      return 0;
  }
  </code></pre>
  
  <p>The Program works fine, but I need to make it somehow that realloc() will give only a maximum size of memory, because at this point it will realloc until User pres ENTER.</p>
  
  <p>This means that i have <strong>6</strong> byte for <strong>Michi</strong> (<strong>5 + 1</strong> for <strong>'\0'</strong>) and the maximum size should be <strong>8</strong> (<strong>7</strong> for <strong>Michael</strong> and <strong>1</strong> for <strong>'\0'</strong>).</p>
  
  <p>How can I do that?</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>I hope you understand now.</p>
  
  <p>My program accept only names with <strong>5</strong> letters.
  At some point I decided that may program should accept names with max <strong>10</strong> Letters.
  I allocated already memory for 5 (michi).
  If you type Michael we have 7 letters so the program should allocate another two for Michael.
  But if i type Schwarzernegger, well this name is too long i do not accept it so i need to make my program to allocate memory only for michael.</p>