Cleaning up my code- how to fix warnings?

I have a bunch of warnings like this in my working code:

D:\River Documents\Arduino\Projects\Telephone\src\Dial\Dialer.ino\sayTemp_.ino:68:24: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]

     sendWav("minus.wav");

                        ^

D:\River Documents\Arduino\Projects\Telephone\src\Dial\Dialer.ino\sayTemp_.ino:83:24: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]

   sendWav("degrees.wav");

                        ^

D:\River Documents\Arduino\Projects\Telephone\src\Dial\Dialer.ino\sayTemp_.ino:86:25: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]

   sendWav("fahrn_hi.wav");

The sendwav function is:

// ================================== sendWav() ==================================
// This function sends an audio filename to the slave device.
// In this case an Arduino Uno with the Adafruit Audio shield.
//

void sendWav(char *f) {
  int rv;                                     // Return Value from Wire.write
  if (strcmp(lastWavFile, f)) {               // Prevents sending the same file twice.
#ifdef DBUG
    Serial.print(F("sendWav: '"));
    Serial.print(f);
    Serial.println(F("'"));
#endif
    Wire.beginTransmission(slaveAddress);     // begin with device address 8
    rv = Wire.write(f);                                  // send the filename
    Wire.endTransmission();                         // stop transmitting
    delay(200);                                           // Wait for latency from the shield.
  }

  strcpy(lastWavFile, f);
  wavFile[0] = '\0';

}

What is the proper way to code this? Is my problem with the call or the function?

Change this line:

void sendWav(char *f) {

to this:

void sendWav(const char *f) {

Thanks- that cleared up a lot of warning lines in my code.

Am I understanding that the argument has to be a const to assure that it can't be modified by the function?

SteveMann:
Am I understanding that the argument has to be a const to assure that it can't be modified by the function?

Yes. The 'const' provides some assurance the the function won't try to modify the memory containing your string constant. You get the warning because, inside the function, the "const char *" becomes a "char *" and the function can write into that memory without triggering any warnings or errors.

SteveMann:
Am I understanding that the argument has to be a const to assure that it can't be modified by the function?

Things to know:

1. const is a tricky type qualifier

const char* is a pointer (*) to a const char: the value being pointed to can't be modified but the pointer can change.

char* const is a constant pointer (*) to a char: the value being pointed at can change but the pointer can't (similar to a reference).

const char* const is a constant pointer (*) to a constant char (so nothing can be modified).

(and to make things complicated, C and C++ do not have the exact same rules but here you are using C++)

2. Compiler tries to help with memory usage
So objects declared with const-qualified types may be placed in read-only memory by the compiler (as they are constant, they don't need to live in RAM).

3. string literal
When you write "degrees.wav" or "minus.wav" you are using string literals and they are const in C++ (after C++11).


So once you understand this, the reason you had a warning was because you were passing a const char* to a function whose signature was not telling the compiler if the data would be modified or not (it was not flagged as const). So the compiler was not sure if what you were trying to do would work or not.

By changing the signature and adding const, you are making a promise to the compiler that the function won't modify the data and then the compiler knows all will be fine and you don't get the warning

  1. Compiler tries to help with memory usage
    So objects declared with const-qualified types may be placed in read-only memory by the compiler

Not on (most) AVRs, though :frowning:
(it is true on the new ATmega4809 boards like Uno WiFi2 and Nano Every. Isn’t that neat!)

westfw:
Not on (most) AVRs, though :frowning:
(it is true on the new ATmega4809 boards like Uno WiFi2 and Nano Every. Isn't that neat!)

yes indeed - it's a general comment. ("tries" - not always succeed :slight_smile: )

J-M-L:
Things to know:

Thanks. I try to learn something every day- now, I can take the rest of the week off.
(Really good info).