void setup() {
Serial.begin(115200);
printOnDisplay("This is a test");
}
void printOnDisplay(char *string) {
string[4] = 0;
Serial.println(string);
}
on an ARDUINO DUE (and maybe on other ARDUINOS, too) the output is not as expected "This", but "This is a test"!? Why is the character-array "This is a test" not changeable. - Is it located in flash-memory?
Thanks for the answers in advance!
So, apparently, on the DUE, literals are stored in a read-only portion of memory. I'd expect an attempt to alter a read-only portion of memory to generate an error at compile time - or at least a warning. But, since we are to be "protected" from our mistakes, warnings are disabled, so we never see them. Pretty stupid, in my opinion.
PaulS:
So, apparently, on the DUE, literals are stored in a read-only portion of memory. I'd expect an attempt to alter a read-only portion of memory to generate an error at compile time - or at least a warning. But, since we are to be "protected" from our mistakes, warnings are disabled, so we never see them. Pretty stupid, in my opinion.
PaulS:
So, apparently, on the DUE, literals are stored in a read-only portion of memory. I'd expect an attempt to alter a read-only portion of memory to generate an error at compile time - or at least a warning. But, since we are to be "protected" from our mistakes, warnings are disabled, so we never see them. Pretty stupid, in my opinion.
...I added the "problem" to the bug-report list on GitHub. Maybe it is not a bug, but at least surprising inconsistency!
This is a known "gotcha" with C/C++. Back in the day before C had the "const" keyword, a read-only string literal was passed around as a pointer to char. You weren't allowed to modify the string, but that couldn't be enforced by the compiler--it had no way to differentiate a string that could be modified from one that couldn't when all you had was a pointer to char.
So now we have a language that still permits you to pass a read-only string literal to a function that takes a pointer to char without throwing a compiler error for the sake of backwards compatibility.
In any case, modifying a string literal is undefined behavior. It's not a bug as it's perfectly legal for the Uno to have different behavior than the Due. It would also be legal for the program to crash, if the implementation happened to work that way.