title: Why can I change the value of a const char* variable?
body: <p>Why does the following code in C work?</p>
  
  <pre><code>const char* str = NULL;
  str = "test";
  str = "test2";
  </code></pre>
  
  <p>Since str is a pointer to a constant character, why are we allowed to assign it different string literals?  Further, how can we protect str from being modified?  It seems like this could be a problem if, for example, we later assigned str to a longer string which ended up writing over another portion of memory.</p>
  
  <p>I should add that in my test, I printed out the memory address of str before and after each of my assignments and it never changed.  So, although str is a pointer to a const char, the memory is actually being modified.  I wondered if perhaps this is a legacy issue with C?</p>