title: String constants vs char arrays in C
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/1704407/what-is-the-difference-between-char-s-and-char-s-in-c">What is the difference between char s[] and char *s in C?</a>  </p>
  </blockquote>
  
  
  
  <p>More of a general question rather than trying to fix something, I've been reading the C programming language book and they take care to make the distinction between</p>
  
  <pre><code>char amessage[] = "blah";
  char *pmessage = "blah";
  </code></pre>
  
  <p>The difference being one is a char array and the other a pointer to a string constant. They say modifying the char array is acceptable but you shouldn't modify string constants as it triggers undefined behavior. My question is: isn't the string constant stored in memory the same way the char array is? Why can I modify it as in</p>
  
  <pre><code>char *p = "this is a string constant";
  *(p+2) = 'a';
  printf("%s", p);
  </code></pre>
  
  <p>Ends up printing "thas is a string constant" as you might expect. I can understand how it would make sense as a string <em>constant</em> shouldn't end up being changed at run time, as it might confuse others/yourself working on your code not expecting it's value to change but in purely functional terms what is wrong with it, what is the undefined behavior that might trigger and how mechanically could it backfire when a char array wouldn't? I'm just wondering if I'm missing something about how string constants work in memory and how they are seen by the compiler.</p>