-1

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).

Pirx
  • 9
  • 2
  • 1
    what is the question? max SSID length is 32 characters, why do you allocate 128? – Juraj Jul 23 '21 at 10:12
  • Why do you write `(char*)cbuffer` ? Any problem with `strcpy( cbuffer, StrGatewaySSID.c_str() );` instead? – datafiddler Jul 23 '21 at 11:10
  • as stated, it is just an example for clarification, because almost all posts about this topic are misleading or ambiguous, and it's for optionally or arbitrarily even longer Strings for different purposes up to that size. – Pirx Jul 23 '21 at 11:11
  • @Pirx: Eventually it's interesting that often one does not either need the `strcpy` ( in case a `const char*` is sufficient. And that there are better variants of `strcpy`, helping to avoid buffer overflows. – datafiddler Jul 23 '21 at 11:14
  • indeed it also works without explicite type casting (char*) – Pirx Jul 23 '21 at 11:15
  • 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. – Pirx Jul 23 '21 at 11:16
  • Of course one may wish to use strncpy alternatively, but the example does not claim to do all and everything as complicated as possible. – Pirx Jul 23 '21 at 11:21
  • 1
    To be nitpicky: the method `c_str()` isn't converting anything, but returns the internal String content as a `const char*` and `strcpy` copies that to a provided (non-const) char buffer. – datafiddler Jul 23 '21 at 11:22
  • You misunderstood: I didn't claim that c_str() is converting anything, my topic is about converting a C++ String to a C string USING the .c_str() method. – Pirx Jul 23 '21 at 11:30

1 Answers1

0

I know your intension is trying to share "best practise" of using String.c_str(), but I couldn't help to say that your "answer" is exactly the one that cause confusion, misleading or ambiguous that you said. If anyone find String.c_str() usage is misleading or ambiguous, it is because one probably does not read the c_str() on Arduino Reference, or fully understand the pointer well.

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.

String.c_str() return a pointer to char array (i.e. a array of char terminated with a '\0'), in another word const char* ptr = String.c_str() represented exactly what String.c_str() is about. Noticed that it is directly access to the buffer in the String object and you are not supposed to change it. So in most case, you use it like Serial.print(ptr). You only need to get a copy of it (as show in your strcpy) if you want to modified the content of your 'copy'.

To put it simply, if the char array (i.e. String.c_str()) is the banana you want to get from a jungle (i.e. the String object) which consists of the forest, the monkey and the whole nine yards, the String.c_str() point you directly to the banana, nothing more, nothing less.

hcheung
  • 3,377
  • 3
  • 11
  • 23
  • that is hollow chatter, you are not providing any useful practice anyway. The crucial point is to have to use strcpy() for assigning a variable String to a variable char[], and that's what not had been clearly stated in different posts and threads. – Pirx Jul 25 '21 at 07:35
  • If my answer in any way offence you, I apologise. If you understand the `char*` (pointer to a char array), you will know that it is the same as `char[0]` (the beginning of a char array). So there is nothing to stop from using it as a normal `char[]` array. You only need `strcpy` if 1) you want to change the content; 2) you worry that the `String` object might be free-up and no long exist (String is created dynamically on the heap and could be free-up when out of the scope). – hcheung Jul 25 '21 at 07:58
  • I did understand it, you don't have to explain ME anything, but many Arduino (!) users don't, so your chatter is pointless and pure know-it-all. My point was to demonstrate a working code, just using strcpy(). – Pirx Jul 25 '21 at 08:19