Bug with character arrays

Ok, I am having a serious problem with character arrays in my string. I wonder if anyone else has encountered this and what they have done.

    Serial.println(txt);
    Serial.write(txt[0]);Serial.write(txt[1]);Serial.write(txt[2]);Serial.write(txt[3]);Serial.write(txt[4]);Serial.println();
    txt[3] = '3';
    Serial.println(txt);
    Serial.write(txt[0]);Serial.write(txt[1]);Serial.write(txt[2]);Serial.write(txt[3]);Serial.write(txt[4]);Serial.println();
    txt[3] = '4';
    Serial.println(txt);
    Serial.write(txt[0]);Serial.write(txt[1]);Serial.write(txt[2]);Serial.write(txt[3]);Serial.write(txt[4]);Serial.println();
    txt[3] = '3';

Leads to the following output:

0
0 12
0
0 13
0
0 14

As you can see, the character arrays are not working! I have been trying to figure this out for a while, as well as figure out the best way to frame it. Frankly, it looks like the compiler is hosed -- and I'm not sure where to go from here.

I have tried compiling in older versions (Arduino 1.0). I am using Arduino 1.5. I also tried it in the newest Beta Arduino (1.5 r_2 I think). Nothing helps, there is always this error.

You should tell us what you want to do.
I see only a part of the sketch, and I don't know what 'txt' is filled with at the start.

Looks like txt starts out like this:
txt[0] = '0';
txt[1] = 0;
txt[2] = '1';
txt[3] = '2';
txt[4] = 0;

Nothing wrong with the compiler at all.

Pete

Ok, I figured out what was happening

1st -- the value of 0 shows up as a space when you print it! I wasn't aware of this "feature" before. So you are right, the second value was a zero... but you are wrong that I initialized it that way.
2nd -- I should have included the above code. The code was txt = "0 12". However, I was using a function which modified the buffer.

Here is where it gets interesting. Once I modified the buffer of a constant character array... very few of the character arrays after it would work! This was happening even if I reset the pointer to "0 14" or something similar -- that second value would be always taken as 0.

I think there is something funny in the compiler (probably to save space) where you absolutely should not modify a constant character array -- once you do, you can never again know what ANY constant character arrays do!

Thanks for your help guys.

I think there is something funny in the compiler (probably to save space) where you absolutely should not modify a constant character array -- once you do, you can never again know what ANY constant character arrays do!

No, there isn't. Post your code, and we'll help you you figure out what you are doing wrong.

It leads to undefined behaviour when you modify a constant string. The compiler reserves the right to store them in any way it sees fit, including re-using constant strings that are the same so only one gets stored in memory. You should consider a constant string to be read only.

Thanks, somehow I didn't realize that -- which is what led to some very "undefined behavior" ]:smiley:

Well, until we see actual code, we'll never know, will we?

cloudformdesign:
Ok, I figured out what was happening

1st – the value of 0 shows up as a space when you print it! I wasn’t aware of this “feature” before. So you are right, the second value was a zero… but you are wrong that I initialized it that way.
2nd – I should have included the above code. The code was txt = “0 12”. However, I was using a function which modified the buffer.

Here is where it gets interesting. Once I modified the buffer of a constant character array… very few of the character arrays after it would work! This was happening even if I reset the pointer to “0 14” or something similar – that second value would be always taken as 0.

I think there is something funny in the compiler (probably to save space) where you absolutely should not modify a constant character array – once you do, you can never again know what ANY constant character arrays do!

Thanks for your help guys.

Without seeing your code, my guess is that Print (Print.cpp in the Arduino core) sees your text array as a string and is trying to print it as such, starting from the “offset” you gave it.

For example, imagine this:

const char *string = “hello there”;
Serial.print (string[1]);

You think you would get “e”, but you get “ello there”. To get the result you want you have to typecast the string to tell Print what you want it to do:

Serial.print ((char) string[1]);

…will result in it printing “e”.

Make sense?

const char *string = "hello there";
Serial.print (string[1]);

I would expect to see 'e'.
I'd be upset if I saw anything else.