-->
Page 1 of 2

Compile error - cannot convert 'String' to 'const char*'

PostPosted: Fri Jan 22, 2016 10:49 pm
by anythingwithawire
I am trying to run the Wifi Scanner example from - https://www.hackster.io/rayburne/warwalking-a9c021

It would not compile after downloading it, I had to insert function prototypes. Then I commented out a few bits and got it mostly going but stuck with a strcpy function giving a compile error.

Error is "WiFiScanOpen:108: error: cannot convert 'String' to 'const char*' for argument '2' to 'char* strcpy(char*, const char*)' strcpy (WiFiSSID, WiFi.SSID(i));"

Relevant code snippits are

Code: Select all#include "ESP8266WiFi.h"
...
int n = WiFi.scanNetworks(); 
...
strcpy (WiFSSID, WiFi.SSID(i));     <== This line giving the compile error, it is inside a for loop for i


I also get compiler warnings for lines like the following, but they don't stop it compiling or running.

Code: Select allsendStrXY("No Nets in Range", ln++, 0);


Warning is

WiFiScanOpen.ino:77:42: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings] sendStrXY("No Nets in Range", ln++, 0);

Can anyone help?

Re: Compile error - cannot convert 'String' to 'const char*'

PostPosted: Sun Jan 31, 2016 5:41 am
by anythingwithawire
I made a workaround, basically my own strcpy function but inline because it was quick and dirty for a once use only in my sketch.

Depending on what you are doing you might like to append a "\n" but I am writing to a display that is only 16 chars wide, so don't really care.

Code: Select all        for (uint8_t x=0; x<16; x++)
          {
          WiFiSSID[x+1]= WiFi.SSID(i)[x];
          }

Re: Compile error - cannot convert 'String' to 'const char*'

PostPosted: Sun Jan 31, 2016 12:02 pm
by martinayotte
There is the String.c_str() function to get the pointer.
So, you strcpy() should be written like that :
Code: Select allstrcpy (WiFSSID, WiFi.SSID(i).c_str());

Re: Compile error - cannot convert 'String' to 'const char*'

PostPosted: Thu Feb 04, 2016 11:13 am
by mrburnette
@martinayotte

Thank you. I've been off line for a few days upgrading everything I can find from Windows to Linux and I have not been diligent about the forums.

Your response has a small typo, should read:
Code: Select allstrcpy (WiFiSSID, WiFi.SSID(i).c_str());


@anythingwithawire & EVERYONE:
The compiler under Arduino 1.6.7 is enforcing much stricter syntax than previously (1.6.5 in my case.) If you use multiple tabs in projects and those tabs default to .INO, then prototype functions are required now. This is somewhat of a departure from the automatic prototypes of previous versions - therefore, it may be a bug in the IDE or the IDE team just getting serious about my sloppy code :cry:

Sorry for the issues, but a lots of my Hackster.io stuff was written to be compliant with Arduino IDE and therefore may break under the current stricter standards.

Ray

Other things you may see:
Under 1.6.6
Sketch uses 236,116 bytes (45%) of program storage space. Maximum is 524,288 bytes.

Under 1.6.7:
Sketch uses 236,961 bytes (54%) of program storage space. Maximum is 434,160 bytes.

Previously:
Arduino 1.6.5r2
Sketch uses 236,100 bytes (45%) of program storage space. Maximum is 524,288 bytes.
Arduino 1.6.1
Sketch uses 235,032 bytes (44%) of program storage space. Maximum is 524,288 bytes.