because many posts about this problem are misleading or ambiguous, this is how it works, just for the record (tested): How to convert an Arduino C++ String to an ANSI C string (char* array) using the String method .c_str()
String myString = "WLAN_123456789"; // Arduino String
char cbuffer[128]=""; // ANSI C string (char* array); adjust size appropriately
strcpy( cbuffer, myString.c_str() );
example (tested):
void setup() {
Serial.begin(115200);
delay(1000);
Serial.println();
String myString = "WLAN_123456789";
char cbuffer[128]="";
Serial.println(myString); // debug
strcpy( cbuffer, myString.c_str() );
Serial.println(cbuffer); // debug
}
void loop() {
}
PS: the example is for variable Strings and for variable char arrays which are not constant, to be able to be assigned to repeatedly and arbitrarily (also for constant strings optionally, too).
(char*)cbuffer? Any problem withstrcpy( cbuffer, StrGatewaySSID.c_str() );instead?strcpy( in case aconst char*is sufficient. And that there are better variants ofstrcpy, helping to avoid buffer overflows.c_str()isn't converting anything, but returns the internal String content as aconst char*andstrcpycopies that to a provided (non-const) char buffer.