What does this error means? I can't solve it in any way.
warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]
What does this error means? I can't solve it in any way.
|
|||
|
Example:
Warning:
The function The compiler is warning you not to do this. Being deprecated it might turn from a warning into an error in a future compiler version. Solution: Make foo take a const char *:
|
|||||||||
|
Either stop trying to pass a string constant where a function takes a String like "random string" are constants. |
|||||||||
|
As is my wont, I'm going to provide a bit of background technical information into the whys and wherefores of this this error. I'm going to inspect four different ways of initializing C strings and see what the differences between them are. These are the four ways in question:
Now for this I am going to want to change the third letter "i" into an "o" to make it "Thos is some text". That could, in all cases (you would think), be achieved by:
Now let's look at what each way of declaring the string does and how that First the most commonly seen way: Basically that means "Warning: You have tried to make a variable that is read-write point to an area you can't write to". If you try and then set the third character to "o" you would in fact be trying to write to a read-only area and things won't be nice. On a traditional PC with Linux that results in:
Now the second one:
It worked, perfectly. Good. Now the third way:
It won't even compile. Trying to write to read-only memory is now protected because we have told the compiler that our pointer is to read-only memory. Of course, it doesn't have to be pointing to read-only memory, but if you point it to read-write memory (RAM) that memory will still be protected from being written to by the compiler. Finally the last form:
So, a quick summary of where we are: This form is completely invalid and should be avoided at all costs. It opens the door to all sorts of bad things happening:
This form is the right form if you are wanting to make the data editable:
This form is the right form if you want strings that won't be edited:
This form seems wasteful of RAM but it does have its uses. Best forget it for now though.
|
|||||
|
To elaborate on Makenko's excellent answer, there is a good reason why the compiler warns you about this. Let's make a test sketch:
We have two variables here, foo and bar. I modify one of those in setup(), but see what the result is:
They both got changed! In fact if we look at the warnings we see:
The compiler knows this is dodgy, and it is right! The reason for this is, that the compiler (reasonably) expects that string constants don't change (since they are constants). Thus if you refer to the string constant |
|||
|