user30152: In my project I need to convert a string to a char array. From various examples I concluded that toCharArray() will convert a string to a char array. But I failed to do so. The error is:

'a' does not name a type

The code I am using is:
String a = "45317";
char b[6];
a.toCharArray(b,6);

Resources are https://www.arduino.cc/en/Reference/StringToCharArray and http://forum.arduino.cc/index.php?topic=199362.0

user4143: If you're trying to use a method of a in the global scope, that's doomed to failure. You can only call methods within functions.
If all you want is a char array with "45317" in it then just use:
char *b = "45317";

If you want to convert a string that is built at runtime into a char array, then your current method is correct - you just have to do it in the right place.

user-1: There's a built in conversion which will return the underlying string-contents as a NULL terminated character array:
 String foo = "Steve was here"
 char *text = foo.c_str();

That is probably all you need, unless you do want to copy into a buffer.  In that case you can use the standard C library to do that:
 // Declare a buffer
 char buf[100];

 // Copy this string into it
 String foo = "This is my string"
 snprintf( buf, sizeof(buf)-1, "%s", foo.c_str() );

 // Ensure we're terminated
 buf[sizeof(buf)] = '\0';

(You might prefer strcpy, memcpy, etc to snprintf.)

30152: <p>In my project I need to convert a string to a char array. From various examples I concluded that <code>toCharArray()</code> will convert a string to a char array. But I failed to do so. The error is:</p>

<blockquote>
  <p>'a' does not name a type</p>
</blockquote>

<p>The code I am using is:</p>

<pre><code>String a = "45317";
char b[6];
a.toCharArray(b,6);
</code></pre>

<p>Resources are <a href="https://www.arduino.cc/en/Reference/StringToCharArray" rel="nofollow noreferrer">https://www.arduino.cc/en/Reference/StringToCharArray</a> and <a href="http://forum.arduino.cc/index.php?topic=199362.0" rel="nofollow noreferrer">http://forum.arduino.cc/index.php?topic=199362.0</a></p>
 4143: <p>If you're trying to use a method of <code>a</code> in the global scope, that's doomed to failure. You can only call methods within functions.</p>

<p>If all you want is a char array with "45317" in it then just use:</p>

<pre><code>char *b = "45317";
</code></pre>

<p>If you want to convert a string that is built at runtime into a char array, then your current method is correct - you just have to do it in the right place.</p>
 -1: <p>There's a built in conversion which will return the underlying string-contents as a NULL terminated character array:</p>

<pre><code> String foo = "Steve was here"
 char *text = foo.c_str();
</code></pre>

<p>That is probably all you need, unless you do want to copy into a buffer.  In that case you can use the standard C library to do that:</p>

<pre><code> // Declare a buffer
 char buf[100];

 // Copy this string into it
 String foo = "This is my string"
 snprintf( buf, sizeof(buf)-1, "%s", foo.c_str() );

 // Ensure we're terminated
 buf[sizeof(buf)] = '\0';
</code></pre>

<p>(You might prefer <code>strcpy</code>, <code>memcpy</code>, etc to <code>snprintf</code>.)</p>
