简体   繁体   中英

Why does Arduino have two different behavior for character pointer processing for Mega 2560 and ESP8266?

I use a same program for MEGA2560 and ESP8266 in Arduino IDE. When compile and upload code to Mega everything is well. But for ESP8266 code is not compiling.

The code is:

...
String s = F("TEl:+12345678912");
scrollText(3, s.c_str(), 1, 20);    
...
void scrollText(byte row,char *p,byte space,int time_delay)
{
 ...
}

And the error is:

initializing argument 2 of 'void scrollText(byte, char*, byte,int)' [-fpermissive]

invalid conversion from 'const char*' to 'char*' [-fpermissive]
     scrollText(3, s.c_str(), 1, 20);

The problem is that str::c_str() returns a variable of type const char * , but your function scrollText(...) is expecting a parameter of type char * . This is a narrowing conversion that most often it's a bad idea (modifying pointers can easily lead you to segfaults or crashes at the tiniest slip). You can set some flags in your compiler to ignore this conversion warning, or change the type of the parameter to const char * to match that of the input (which will lead to a different compiler error if later within the function you try to mutate this parameter when it was declared as const). Choose your solution, but may I advise you something: never trust pointers and non-consts unless you really really know what you're doing.

The reason why this compiles for the Arduino but not for the ESP is because in the case of the Arduino, the compiler is being launched with the -fpermissive flag on by default (which ignores this kind of errors), where the ESP compiler is not. This is an Arduino flaw, apparently some very old libraries where compiled with this flag on, and now they don't want to remove it for fear of disrupting old code. You should never need this flag, it's just there for legacy purpose, but today we know better.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM