1

I'm using DS3231 module with rinkydink library and it is working just fine, however every time i want to compile the code Arduino IDE shows this warning:

C:\Program Files (x86)\Arduino\libraries\DS3231\DS3231.cpp:291:106: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]

What is this warning and how to solve it?

  • You're passing a string constant to a function that expects a variable. – Majenko Jun 20 at 12:59
  • 2
    But often it's library writer's mistake and that parameter could (and should) be const because there is no write access to it. – KIIV Jun 20 at 13:34
  • why do you use this old library? – Juraj Jun 20 at 13:59
  • 1
    On the face of it your argument sounds fine. But looking at the nuts and bolts of why a const is used makes it a bit more obvious why this is a warning. When a compiler encounters a const, it might try putting that value into (read only) program memory. After all, you promised not to change it. But passing it as a formal value may be interpreted as you wanting to create a variable in the called to function. So there can be confusion or a disconnect in what the compiler does and what you expect to happen. – st2000 Jun 20 at 14:03
  • 2
    I have started building up a list of common errors and their explanations: majenko.co.uk/blog/common-arduino-errors-and-warnings – Majenko Jun 20 at 16:31
2

The warning was caused by this line:

char *monthShort[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};

In C++, the content of a character string such as "Jan" cannot be modified. Thus, if you assign the string to a pointer, the pointer should be declared as const char *:

const char *monthShort[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
                            "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};

The same applies to the other arrays of strings: daysLong, daysShort and monthLong. As a consequence, the prototypes of the methods getDOWStr() and getMonthStr() should be changed in order to return a const char *.

BTW, are you sure this library is still maintained? This should have been fixed a long time ago, and it seems the source was last modified in 2015. You may be better off using a more modern alternative. For instance, in the Arduino library manager, you can find DS3231 and Adafruit's RTClib.

  • i use this library because i'm very new to arduino and everyone every where suggested this and it's easy to use, i'll try the new libraries. thanks. – Hamed Jun 20 at 14:13
0

The DS3231 library from Rinky Dink is the best RTC library there is...I know, I tried them ALL. This library allows one to print date and time strings with only 3 Arduino print statements: Serial.print(rtc.getDateStr()); Serial.print(" -- "); Serial.println(rtc.getTimeStr()); Other RTC libraries force user to make the strings up themselves, at a cost of about 12 lines of useless Serial.print commands.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.