I have successfully used simple arrays in the past but never tried to update them... just read them.
So now I wrote this simple little sketch but as in this example, when I update the 3rd element with 'bbbbb', (OK). Then when I update then 4th element with 'ccccc' both it and the 3rd element are changed to 'ccccc'. Here I have updated 3 and 4 in that order but the same thing happens if I update them in the reverse order.
Update: After posting this I found that in the 'results' below the sketch, elements 3-4 are ccccc but when I copied them here they show as aaaaa (the original contents of the element). Huh?
Wow... thanks for the fast reply. As you may have guessed, I am something of a lightweight here. Not real strong on using pointers. But here is my problem... in my 'real' sketch I am attempting to fill a small array with time/date stamps and keep just the last several entries for display. Could you suggest how I would accomplish that?
@fatfenders
All your variables except of f1 are pointers to the string literals. literals are not meant to be changed after creation, so the compiler can use the same literal for multiple variables.
So you shouldn't try to assign a new values to it.
If your string variable need to be changed during the program run - use a char arrays instead of literals:
char* a = "aaaaa";
char* b = "aaaaa";
char c[] = "bbbbb";
char d[] = "bbbbb";
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
Serial.println(" === Before === ");
Serial.print(" a = "); Serial.println(a);
Serial.print(" b = "); Serial.println(b);
Serial.print(" c = "); Serial.println(c);
Serial.print(" d = "); Serial.println(d);
a[2] = '_';
c[2] = '_';
Serial.println(" === After === ");
Serial.print(" a = "); Serial.println(a);
Serial.print(" b = "); Serial.println(b);
Serial.print(" c = "); Serial.println(c);
Serial.print(" d = "); Serial.println(d);
}
void loop() {
// put your main code here, to run repeatedly:
}
Output:
=== Before ===
a = aaaaa
b = aaaaa
c = bbbbb
d = bbbbb
=== After ===
a = aa_aa
b = aa_aa
c = bb_bb
d = bbbbb
Thanks again. Your advice coupled with time spent going back and reading/understanding arrays, pointers, etc. got me back on track. I solved it by going to a 2D array :