user42: When reading/trying a recent answer, I was surprised to see that Arduino's String class supports the c_str() method, just like the C++ std::string class. As expected, it appears to get a pointer to the string's contents as a null-terminated char array (i.e. C-style string).
However, (as far as I can see) that method is not mentioned in the official Arduino documentation. Additionally, in all example code I've seen using String, a different approach seems to be used. A secondary char buffer is setup, and then the contents of the string are copied to it using String::toCharArray(). This obviously requires double the memory, plus an O(n) copy operation.
It seems like c_str() should be the preferred approach. Is there some reason why toCharArray() is more commonly used?

user379: 
It seems like c_str() should be the preferred approach. Is there some reason why toCharArray() is more commonly used?

basically, I'd say it's a lack of knowledge from the people doing the codes you've seen. Definitely c_str() is better. Though, what I see even more often is the use of character arrays char* strings instead of String (and I plead guilty of that as well in my own codes).
And that's because the Arduino library has been built upon a messy set of C and C++ libraries and coding style. Trying to make things easier actually messed them up and complicated them more.
That's actually why we have new projects like xpcc trying to make a real and smart use of the C++ abilities in the embedded world.

42: <p>When reading/trying a <a href="https://arduino.stackexchange.com/a/511/42">recent answer</a>, I was surprised to see that Arduino's <code>String</code> class supports the <code>c_str()</code> method, just like the C++ <code>std::string</code> class. As expected, it appears to get a pointer to the string's contents as a null-terminated <code>char</code> array (i.e. C-style string).</p>

<p>However, (as far as I can see) that method is not mentioned in the official Arduino documentation. Additionally, in all example code I've seen using <code>String</code>, a different approach seems to be used. A secondary <code>char</code> buffer is setup, and then the contents of the string are copied to it using <a href="http://arduino.cc/en/Reference/StringToCharArray" rel="nofollow noreferrer"><code>String::toCharArray()</code></a>. This obviously requires double the memory, plus an O(n) copy operation.</p>

<p>It seems like <code>c_str()</code> should be the preferred approach. Is there some reason why <code>toCharArray()</code> is more commonly used?</p>
 379: <blockquote>
  <p>It seems like <code>c_str()</code> should be the preferred approach. Is there some reason why <code>toCharArray()</code> is more commonly used?</p>
</blockquote>

<p>basically, I'd say it's a lack of knowledge from the people doing the codes you've seen. Definitely <code>c_str()</code> is better. Though, what I see even more often is the use of character arrays <code>char*</code> strings instead of <code>String</code> (and I plead guilty of that as well in my own codes).</p>

<p>And that's because the Arduino library has been built upon a messy set of C and C++ libraries and coding style. Trying to make things easier actually messed them up and complicated them more.</p>

<p>That's actually why we have new projects like <a href="http://xpcc.kreatives-chaos.com" rel="nofollow">xpcc</a> trying to make a real and smart use of the C++ abilities in the embedded world.</p>
