title: Reversing a string using pointer-arithmetic
body: <p>I want to reverse a string using pointer arithmetic. I wrote a small program based on what I read in "A Book on C" and basic programs I wrote myself (which worked to my satisfaction).
  Here is what I wrote:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  #include &lt;assert.h&gt;
  
  void rv(char* str)
  {
      int l = strlen(str);
      char* copy = (char*) malloc((l + 1) * sizeof(char));
      assert(copy != NULL);
      int i;
  
      for (i = 0 ; i &lt; l ; i++) { // copy str to copy in reversed order
          *(copy + i) = *(str + (l - 1 - i));
      }
  
      for (i = 0 ; i &lt; l ; i++) { // 
          *(str + i) = *(copy + i);
      }
  
      free(copy);
      printf("Current string is %s\n", str);
  }
  
  int main(void)
  {
      char* s1 = "abcde";
      rv(s1); 
      return 0;
  }
  </code></pre>
  
  <p>The program crashes.
  From my tests, the problematic part is:</p>
  
  <pre><code>for (i = 0 ; i &lt; l ; i++) { // 
      *(str + i) = *(copy + i);
  }
  </code></pre>
  
  <p>I read similar questions, and it seems that this little piece of code should do no harm.
  actually, even if I replace that part with:</p>
  
  <pre><code>*(str + i) = 'c';
  </code></pre>
  
  <p>The program still crashes. It seems I can't even change one simple character of the string.
  what is the problem?</p>