Loading...
Pages: [1]   Go Down
Author Topic: String does not work with println. Documentation bug?  (Read 194 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Global Moderator
UK
Online Online
Shannon Member
*****
Posts: 13036
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

In C/C++, you can write
Code:
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:
Code:
void setup() {
  Serial.begin(9600);
  Serial.println(settings[1]);
}
with your second example?
« Last Edit: February 17, 2012, 08:06:51 AM by AWOL » Logged

Pete, it's a fool looks for logic in the chambers of the human heart.

Offline Offline
Newbie
*
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Global Moderator
UK
Online Online
Shannon Member
*****
Posts: 13036
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pete, it's a fool looks for logic in the chambers of the human heart.

Dallas, TX USA
Online Online
God Member
*****
Posts: 935
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
Code:
"string1""string2"
"string1" "string2"
and
Code:
"string1"
"string2"

are all the same as:
Code:
"string1string2"

--- bill


Logged

Global Moderator
UK
Online Online
Shannon Member
*****
Posts: 13036
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Bill, see reply #1
Logged

Pete, it's a fool looks for logic in the chambers of the human heart.

Pages: [1]   Go Up
Print
 
Jump to: