String class: how to assign String.c_str() to a char[] ?

hello, unfortunately in the reference to the String class an example is missing how to assign String.c_str() to a char[] .

e.g., given I have

char s[20]; String Str="abcdefg";

how to assign Str to s? perhaps directly by s= Str.c_str();

or instead only possible by using strcpy()? strcpy( s, Str.c_str() ); :?:

String Str="abcdefg";Why start with a String and convert it when you could use a string and need no conversion ?

I need to convert the original String (from a wifiserver message or a HTTP request) to char because I have to modify it afterwards serveral times, and eventually the original "string" is of a Arduino String (class). But it's a general problem, in case either Arduino libs use Strings instead of char[].

e.g., given I have

char s[20]; String Str="abcdefg";

how to assign Str to s? perhaps directly by s= Str.c_str();

or instead only possible by using strcpy()? strcpy( s, Str.c_str() ); :?:

If you feel that you [u]really must[/u] use Strings then look at the String.toCharArray() function

UKHeliBob: If you feel that you [u]really must[/u] use Strings then look at the String.toCharArray() function

why that? why can't I use Str.c_str(), finally it's there? And eventually I want to get the 0-terminated char[] just until '\0', and not any chunk which perhaps follows after the termination.

OP, you suggested:

strcpy( s, Str.c_str() );

What happened when you tried it?

The c_str() function appears to have severe limitations

From the Arduino reference

Converts the contents of a string as a C-style, null-terminated string. Note that this gives direct access to the internal String buffer and should be used with care. In particular, you should never modify the string through the pointer returned. When you modify the String object, or when it is destroyed, any pointer previously returned by c_str() becomes invalid and should not be used any longer.

I want to get the char[] just until '\0'

The toCharArray() function takes the length of the String as its second parameter so you can control the number of characters converted and returned in the char array and if need be insert the termination '\0' yourself. The Arduino documentation does not make it clear whether the termination is added by the function.

UKHeliBob: The Arduino documentation does not make it clear whether the termination is added by the function.

The code shows that it does.

void toCharArray(char *buf, unsigned int bufsize, unsigned int index=0) const
    { getBytes((unsigned char *)buf, bufsize, index); }

void String::getBytes(unsigned char *buf, unsigned int bufsize, unsigned int index) const
{
    if (!bufsize || !buf) return;
    if (index >= len) {
        buf[0] = 0;
        return;
    }
    unsigned int n = bufsize - 1;
    if (n > len - index) n = len - index;
    strncpy((char *)buf, buffer + index, n);
    buf[n] = 0;
}

gfvalvo: OP, you suggested:

strcpy( s, Str.c_str() );

What happened when you tried it?

actually I want to know how to make it 100% correctly, not have a random result which once may work accidentally and once perhaps may not.

@UKHeliBob: I don't understand your concerns about c_str(). The reference says it returns a pointer to the char[], I now just want to know how to handle that pointer correctly in order to convert the String to a separate char[] variable.

Perhaps someone else can tell me please which is the definite answer to my TOP question?

unfortunately in the reference to the String class an example is missing how to assign String.c_str() to a char[] .

e.g., given I have

char s[20]; String Str="abcdefg";

how to assign Str to s? perhaps directly by s= Str.c_str();

or instead only possible by using strcpy()? strcpy( s, Str.c_str() ); :?:

how to convert strind to char array String a=("1234567890")

aravinddevarajan: how to convert strind to char array String a=("1234567890")

I don't know what strinds are.

But even if you insert string/String, the question does not make sense.

Char arrays, c-strings and Strings all store their data in a byte/char array, only Strings use a function to get the buffer address.

with out using to char array function

yes