String to char[] help

I know the best advice is to avoid Strings like the plague, but I have a library that returns a string. (Arduino/libraries/ESP8266WiFi at master · esp8266/Arduino · GitHub)

I can print the String on the serial monitor, which is fine, but I am trying to write it on an LED display. For the display I need to send a character array.

given a String from the library:

for (int i = 0; i < n; ++i)
{
   Serial.print(WiFi.SSID(i));
}

I thought I could try something like this to convert to an array:

for (int i = 0; i < n; ++i)
{
   char buffer [100];
   String SSIDstring = WiFi.SSID(i); 
   Serial.print(SSIDstring.toCharArray(buffer,100));
}

But I get an error "invalid use of void expression." What is the void expression?

I know I should never use a String, but can I convert a String to a char[]? If so, how?

Thanks!

Jimmy

You need to move the SSIDstring.toCharArray(buffer,100) to outside the Serial.print, put it on a line itself. Then place the buffer in Serial.print.

Thanks Byork.

      char buffer [100];
      String SSIDstring = WiFi.SSID(i);
      SSIDstring.toCharArray(buffer,100);
      Serial.print(buffer);

That seems to do the trick.

1 Like

:slight_smile: