title: How to append '\n' at the end of char*?
body: <p>Here's my code that shows what I'm trying to do but does not work.</p>
  
  <pre><code>#include &lt;iostream&gt;
  
  char * getNewStr(char *str)
  {
      size_t oldsize = strlen(str);
      size_t newsize = oldsize + 2;
      str = static_cast&lt;char *&gt;(realloc(str, newsize));
      str[oldsize] = '\n';
      str[oldsize + 1] = '\0';
      return str;
  }
  
  int main(int argc, const char * argv[])
  {
      char *str = const_cast&lt;char *&gt;("Hello World");
      char *newStr = getNewStr(str);
      std::cout &lt;&lt; newStr &lt;&lt; newStr;
      free(newStr);
      return 0;
  }
  </code></pre>
  
  <p>I expect the result to be</p>
  
  <pre><code>Hello World
  Hello World
  </code></pre>
  
  <p>But currently, this crashes at the <code>realloc()</code> part.</p>
  
  <p>How can I correct the function <code>getNewStr()</code> to make it work?</p>
  
  <p>(P.S: The argument and return type for <code>getNewStr()</code> function should be <code>char *</code> as it is now. I don't want to use <code>std::string</code> in the function.)</p>