void setup() { Serial.begin(115200); printOnDisplay("This is a test");}void printOnDisplay(char *string) { string[4] = 0; Serial.println(string);}
Why is the character-array "This is a test" not changeable.
string[4] = 0;
string[4] = '\0';
Code: [Select]Why is the character-array "This is a test" not changeable.What are you expecting to see, when the function outputs the data? I expect to see "This" followed by a carriage return and line feed.What are you actually seeing?
void setup() { Serial.begin(115200);}void loop() { printOnDisplay("This is a test"); while(1);}void printOnDisplay(char *string){ string[4] = 0; Serial.println(string); if (string[4]) Serial.println("String hasn't changed!"); else Serial.println("String has changed!");}
void loop() { printOnDisplay("This is a test"); while(1);}
void loop(){ char test[] = "This is a test"; printOnDisplay(test); while(1);}
If you change:Code: [Select]void loop() { printOnDisplay("This is a test"); while(1);}toCode: [Select]void loop(){ char test[] = "This is a test"; printOnDisplay(test); while(1);}do you get the same (apparently incorrect) output?
With your change it now works as expected!
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.