Modifying underlying char array of a c++ string object
<p>My code is like this:</p>

<pre><code>
    string s="abc";
    char* pc = const_cast&lt;char*>(s.c_str());
    pc[1]='x';
    cout &lt;&lt; s &lt;&lt; endl;
</code></pre>

<p>When I compiled the snippet above using gcc, I got the result  "axc" as expected. My question is, is that safe and portable to modify the underlying char array of a c++ string in this way? Or there might be alternative approaches to manipulate string's data directly?</p>

<p>FYI, my intention is to write some pure c functions that could be called both by c and c++, therefore they can only accept char* as arguments. From char* to string, I know there is copying involved, the penalty is unfavorable. So, could anybody give some suggestions to deal with this sort of situation.</p>
