Warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]

Hi,
I'm getting this error for 4 days. I nearly tried all solutions that in the internet.

Here's the warning messages:
C:\Users\burka\AppData\Local\Temp\arduino_modified_sketch_615883\SMS_Konum_Uno.ino: In function 'void setup()':
C:\Users\burka\AppData\Local\Temp\arduino_modified_sketch_615883\SMS_Konum_Uno.ino:29:64: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
Kapadokya.smsGonder(gonderilecekTelNumarasi, "Sistem acildi.");
^
C:\Users\burka\AppData\Local\Temp\arduino_modified_sketch_615883\SMS_Konum_Uno.ino:29:64: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
C:\Users\burka\AppData\Local\Temp\arduino_modified_sketch_615883\SMS_Konum_Uno.ino: In function 'void loop()':
C:\Users\burka\AppData\Local\Temp\arduino_modified_sketch_615883\SMS_Konum_Uno.ino:41:66: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
Kapadokya.smsGonder(gonderilecekTelNumarasi, "LED Acildi");
^
C:\Users\burka\AppData\Local\Temp\arduino_modified_sketch_615883\SMS_Konum_Uno.ino:41:66: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
C:\Users\burka\AppData\Local\Temp\arduino_modified_sketch_615883\SMS_Konum_Uno.ino:45:69: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
Kapadokya.smsGonder(gonderilecekTelNumarasi, "LED Kapatildi");
^
C:\Users\burka\AppData\Local\Temp\arduino_modified_sketch_615883\SMS_Konum_Uno.ino:45:69: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
C:\Users\burka\AppData\Local\Temp\arduino_modified_sketch_615883\SMS_Konum_Uno.ino:72:60: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
Kapadokya.smsGonder(gonderilecekTelNumarasi, konum2);

and here's my code:

//Kapadokya GSM Shield : http://bit.ly/GSMshield
//─░nceleme ve proje videosu: https://youtu.be/zijlJ2-lzhg

//kodu y├╝kledikten sonra gps mod├╝l├╝n├╝ ba─člamak gerekiyor

#include <KapadokyaGSM.h>
KapadokyaGSM Kapadokya;
String smsMetni, smsNumarasi;
#define gonderilecekTelNumarasi  "+90531553xxxx" // Numaray─▒ de─či┼čtirmeyi unutmay─▒n :) 
#define LED_pin 13

#define GPS Serial
#include <TinyGPS.h>
TinyGPS gps;

double enl;
double boy;
String konum;
char konum2[255];

void setup() {
  Kapadokya.basla();
  delay(2000);
  pinMode(LED_pin, OUTPUT);
  GPS.begin(9600);
  delay(2000);
  Kapadokya.tumSmsSil();
  delay(2000);
  Kapadokya.smsGonder(gonderilecekTelNumarasi, "Sistem acildi.");
}

void loop() {
  smsMetni = Kapadokya.smsOku(1);
  if (smsMetni.indexOf("OK") != -1) {
    if (smsMetni.length() > 7) {
      smsNumarasi = Kapadokya.numaraliSmsOku(1);
      smsMetni.toUpperCase();

      if (smsMetni.indexOf("LEDAC") != -1) {
        digitalWrite(LED_pin, HIGH);
        Kapadokya.smsGonder(gonderilecekTelNumarasi, "LED Acildi");
      }
      else if (smsMetni.indexOf("LEDKAPAT") != -1) {
        digitalWrite(LED_pin, LOW);
        Kapadokya.smsGonder(gonderilecekTelNumarasi, "LED Kapatildi");
      }
      else if (smsMetni.indexOf("NERDESIN") != -1 || smsMetni.indexOf("NEREDESIN") != -1 || smsMetni.indexOf("KONUM") != -1) {
        delay(1000);

        bool yeniVeri = false;
        for (unsigned long start = millis(); millis() - start < 1000;) {
          while (GPS.available()) {
            char c = GPS.read();
            if (gps.encode(c))
              yeniVeri = true;
          }
        }

        if (yeniVeri) {
          float flat, flon;
          unsigned long age;
          gps.f_get_position(&flat, &flon, &age);
          enl = flat;
          boy = flon;
          konum = "www.google.com.tr/maps/place/" + String(enl, 6) + "," + String(boy, 6);
          konum.toCharArray(konum2, 100);
          delay(1000);
        }
        delay(1000);
        char konum2[255];
        konum.toCharArray(konum2, 100);
        Kapadokya.smsGonder(gonderilecekTelNumarasi, konum2);
      }
      else {
      }
      Kapadokya.tumSmsSil();
    }
  }
}

How can I fix it?

use

char gonderilecekTelNumarasi[] =   "+90531553xxxx" // Numaray─▒ de─či┼čtirmeyi unutmay─▒n :)

(modified the phone number)

1 Like

The problem is caused by the smsGonder function in the library, it is expecting a char* and you are giving it a const char*. char* indicates to the compiler that the char array is allowed to be modified by the function, so it should not accept a string constant which should not be modified.

indeed.

Hence the answer in #2 that will put the phone number in a modifiable array.

(which was going to be my next question to the OP if that was working: why does it work :wink: ).

I tried the same library, it's not using const char* in that function.

1 Like

poorly written library then...

2 Likes

You can just unconst the const if you know the value pointed to is not modified by the function


Kapadokya.smsGonder(const_cast<char *>(gonderilecekTelNumarasi), "Sistem acildi."); 

"abusing" of the compiler's trust can work but the spec says

Modifying a const object through a non-const access path results in undefined behavior

imagine the const string was in Flash memory and you were on an architecture where you need special functions to go read from Flash, then you'd be transforming the flash memory address into SRAM memory address and of course the information would not be there

Preferably you would report the issue with the library and the maintainers of the library would change the function to accept a const char*.

This particular warning seems to to related to a change in the compiler a few years ago that changed the behavior from a warning to an error, breaking a lot of pre-existing code. The Arduino IDE then starting specifying the fpermission option to tell the compiler to downgrade the error to a warning, in part to give library maintainers time to fix their code, and in part to make the warning visible so that people would notice it. Since the default settings in the IDE have never shown all warnings, the warning have always been there, just noone noticed unless they set the compiler options to show all warnings.

the key word

This is a pet peeve of mine. Many library authors never turn on warning notifications and when things break like this, the original author is nowhere to be found, or can't be bothered to fix their sloppy coding.

I think that especially because the IDE was supposed to be a teaching tool, it makes no sense to not show compiler warnings. The warnings could be errors in future releases of the IDE. If someone is trying to learn C++ using the Arduino IDE, then teach the right way to do things from the start.

Not like you are paying for it

1 Like

Good point, but I would be embarrassed to publish sloppy code. If I use a library that produces a bunch of warnings, then I have to wonder what other coding errors might be compiling in the library.

Not everyone is self aware and or have high standards when it comes to code. Also every John Paul and Ringo is a C++ programmer but cannot tell a pointer from multiplication.

fair point.