title: "Stack around the variable 'buffer' was corrupted" - What is wrong here
body: <p>I want to parse the numbers from an IP address which is entered by the user. I have written the following code to take the input IP address from the user and print each bytes of the address. </p>
  
  <p><strong>CODE:</strong>(Right now I am assuming that the user input is like "xx.xx.xx.xx")</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;string.h&gt;
  void main(){
  
      char ip[16];
      char buffer[6];
      int i=0;
      char temp;
      int len;
      char *ptr1;
      char *ptr2;
      char delim = '.';
      while((temp = getchar()) != '\n')
      {
          ip[i++]=temp;
      }
      ip[i] = '\0';
      ptr2 = ip;
      for(i=0;i&lt;4;i++){
          ptr1 = strchr(ptr2,(int)delim);
          strcpy(buffer,ptr2);
          if(ptr1 != NULL){
              buffer[ptr1-ptr2] = '\0';
              ptr2=ptr1+1;
          }       
          printf("\nString:%s",buffer);
      }
  
  
      getchar();
  
  }
  </code></pre>
  
  <p>The code runs fine but at the end of the running or debugging session it gives the error </p>
  
  <pre><code>Run-Time Check Failure #2 - Stack around the variable 'buffer' was corrupted.
  </code></pre>
  
  <p>What is the problem with my code?</p>