I am using a teensy 4.0 with Visual Studio 2022, and I'm getting a warning that I don't understand.
The following code:
double getInputDouble() { //get a string from serial mon and convert it to a 64bit double
double result = 0.0;
const byte inputLength = 32;
char inputBuffer[inputLength]{}; // an array to store the received data
int s_len = 0;
while (Serial.available() == 0) {} //Wait for input
while (Serial.available()) {
char input = Serial.read();
if (s_len >= inputLength) {
// Have received the maximum number of characters
// Ignore all new input until line termination occurs
}
else if (input != '\n' && input != '\r') {
inputBuffer[s_len] = input; s_len++;
}
else { // Have received a LF or CR character
result = strtod(inputBuffer, NULL);
Serial.printf("%.15e", result); Serial.println(); //print in scientific notation
}
} //end while
return result;
}
Generates this warning:
warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
122 | double getInputDouble() { \\get a string from serial mon and convert it to a 64bit double
| ^~~~~~~~
serialmon.ino: 123:23: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
123 | double result = 0.0
| ^~
I think you may run into trouble with your logic. There is a possibility that the characters will come in too slow and your while() loop can fall out before you get 'em all.
Also you do realize this function is blocking as heck.
Let me refocus everyone: There is nothing wrong with the logic. No characters are going to fall off because I have set Serial.timeout appropriately in my setup() elsewhere. I want it to be "blocking as heck". There is nothing wrong with my comments. The code works exactly as I want it to.
To remind everyone of the question: What in this code is generating the compiler warning?
Simple fact: C++ String is a Container Class that wraps wasted RAM around a char array string so that people who learned Basic or Pascal first can "plus" text without approaching text as what it is which C does very well with minimal waste.
That cannot compile as a string. You String has an operator to tell you where your text is held captive where it HAS TO BE KEPT SAFE FROM YOU.
If you don't have RAM and cycles to waste, don't use String variables.
(Interestingly, your function compiles fine in the Arduino IDE, even with -Wall enabled. So perhaps the error IS elsewhere in your sketch.)
The warning comes about when "old" C-string functions (declared to take "char *" arguments are passed a literal string constant, which in C++ is "const char *" (did you know that such functions could theoretically (and "legally") modify the string literal, back in the day?) User functions that don't modify a string argument should declare it as "const char *" as well.
It should not matter if you pass a char* to a function that wants a const char*, it does no harm that the function promises not to modify the data pointed to by a char*. The other way around, passing a const char* to a function that wants a char* that it can modify, is where you get into trouble.