title: Why doesn't strcpy work?
body: <pre><code>    char sentence2[10];
  
      strncpy(sentence2, second, sizeof(sentence2));  //shouldn't I specify the sizeof(source) instead of sizeof(destination)?
  
      sentence2[10] = '\0';                       //Is this okay since strncpy does not provide the null character.
  
      puts(sentence2);
  //////////////////////////////////////////////////////////////
  
      char *pointer = first;
      for(int i =0; i &lt; 500; i++)                 //Why does it crashes without this meaningless loop?!
      {
          printf("%c", *pointer);
          if(*pointer == '\n')
              putchar('\n');
          pointer++;
      }
  </code></pre>
  
  <p>So here's the problem. When I run the first part of this code, the program crashes.
  However, when I add the for loop that just prints garbage values in memory locations, it does not crash but still won't strcpy properly. </p>
  
  <p>Second, when using strncpy, shouldn't I specify the sizeof(source) instead of sizeof(destination) since I'm moving the bytes of the source ?</p>
  
  <p>Third, It makes sense to me to add the the null terminating character after strncpy, since I've read that it doesn't add the null character on its own, but I get a warning that it's a possible out of bounds store from my pelles c IDE. </p>
  
  <p>fourth and most importantly, why doesn't the simply strcpy work ?!?!</p>
  
  <p>////////////////////////////////////////////////////////////////////////////////////</p>
  
  <p>UPDATE: </p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;string.h&gt;
  
  void main3(void)
  {
      puts("\n\n-----main3 reporting for duty!------\n");
      char *first = "Metal Gear";
      char *second = "Suikoden";
      printf("strcmp(first, first)  = %d\n", strcmp(first, first));   //returns 0 when both strings are identical.
      printf("strcmp(first, second) = %d\n", strcmp(first, second));  //returns a negative when the first differenet char is less in first string.  (M=77  S=83)
      printf("strcmp(second, first) = %d\n", strcmp(second, first));  //returns a positive when the first different char is greater in first string.(M=77  S=83)
  
      char sentence1[10]; 
      strcpy(sentence1, first);
      puts(sentence1);
      char sentence2[10];
      strncpy(sentence2, second, 10); //shouldn't I specify the sizeof(source) instead of sizeof(destination).
      sentence2[9] = '\0';                        //Is this okay since strncpy does not provide the null character.
  
      puts(sentence2);
      char *pointer = first;
      for(int i =0; i &lt; 500; i++)                 //Why does it crashes without this nonsensical loop?!
      {
          printf("%c", *pointer);
          if(*pointer == '\n')
              putchar('\n');
          pointer++;
      }
  }
  </code></pre>
  
  <p>This is how I teach myself to program. I write code and comment all I know about it so that
  the next time I need to look up something, I just look at my own code in my files. In this one, I'm trying to learn the string library in c.</p>