String does not work with println. Documentation bug?

There is an inconsistency between actual behavior and the arduino documentation. http://arduino.cc/en/Reference/StringConstructor http://arduino.cc/en/Serial/Print

According to the documentation, a String can be initialized using a constant in the form of "some text here". However this fails to add a null terminator to the end of the String. As such this code does not work:

String settings[]={ "CURSOR_DRIVER" "BRLTTY_DRIVER" "BUFFER_SIZE_MAX" "BUFFER_COLUMNS" "BUFFER_ROWS" "DOTCOUNT"};

void setup() { Serial.begin(9600); Serial.println(settings[0]); }

void loop() { // put your main code here, to run repeatedly:

}

It prints: CURSOR_DRIVERBRLTTY_DRIVERBUFFER_SIZE_MAXBUFFER_COLUMNSBUFFER_ROWSDOTCOUNT (Along with no amount of garbage, if there doesn't happen to be a \0 somewhere in the next kilobyte of memory...)

While this code does work:

String settings[]={ "CURSOR_DRIVER\0" "BRLTTY_DRIVER\0" "BUFFER_SIZE_MAX\0" "BUFFER_COLUMNS\0" "BUFFER_ROWS\0" "DOTCOUNT\0"};

void setup() { Serial.begin(9600); Serial.println(settings[0]); }

void loop() { // put your main code here, to run repeatedly:

}

It prints: CURSOR_DRIVER

I must note, that this is really only a problem in documentation. The documentation for the print command clearly states that it works on any data type:

http://arduino.cc/en/Serial/Print val: the value to print - any data type

And yet in reality, it works on NULL terminated strings but not length bounded Strings....

I am on Arch linux.

$pacman -Q arduino arduino 1:1.0-2

$ uname -a Linux timothy 3.2.5-1-ARCH #1 SMP PREEMPT Tue Feb 7 08:46:10 UTC 2012 i686 Intel(R) Core(TM)2 Duo CPU L7500 @ 1.60GHz GenuineIntel GNU/Linux

Timothy Hobbs

Not sure what you're trying to do, but I think your initialisation list is missing some commas.

In C/C++, you can write

char* myString = "This"
" is a "
" long string";

And the compiler will happily concatenate it into "This is a long string".

What happens if you try this:

void setup() {
  Serial.begin(9600);
  Serial.println(settings[1]);
}

with your second example?

Ah, aha, you're right! Thank you. I'm surprised that even compiled. Sorry for the false report. I didn't even see the error in my code.

I have to admit, I assumed the presence of the missing commas on my first reading.

timthelion: Ah, aha, you're right! Thank you. I'm surprised that even compiled. Sorry for the false report. I didn't even see the error in my code.

C concatenates multiple strings into a single string. So:

"string1""string2"
"string1" "string2"

and

"string1"
"string2"

are all the same as:

"string1string2"

--- bill

Bill, see reply #1