edward0429 / arduino

Automatically exported from code.google.com/p/arduino
0 stars 0 forks source link

String.toCharArray() and String.getBtytes are not const #552

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1.
2.
3.

What is the expected output? What do you see instead?

What version of the Arduino software are you using? On what operating
system?  Which Arduino board are you using?

Please provide any additional information below.

Original issue reported on code.google.com by matthew....@gmail.com on 3 Jun 2011 at 9:54

GoogleCodeExporter commented 9 years ago
[Apologies for the initial report - trying again...!]

Even though the implementation of String.toCharArray() and String.getBytes() 
return a copy of the internal buffer they are non-const, which means that 
rather than being able to write code such as:

  const String s("A string");
  char charArray[9];
  s.toCharArray(charArray, 9);

You have to cast away the const-ness (which is a bit messy):

  const String s("A string");
  char charArray[9];
  ((String)s).toCharArray(charArray, 9);

Both functions could be made const without breaking existing calling code and 
simplifying usage in this scenario.

Original comment by matthew....@gmail.com on 3 Jun 2011 at 10:01

GoogleCodeExporter commented 9 years ago
I see that this change has already gone into the 1.0 new-extension branch as a 
result of the String class changes.

Original comment by matthew....@gmail.com on 7 Jun 2011 at 9:03

GoogleCodeExporter commented 9 years ago
I know it's been a while.. perhaps you remember this?
did you ever get the string to work?

see error in comment

Thank you!

my code:

//from SerialEvent
const String g("geiger");
String inputString = "g";         // a string to hold incoming data
  char charArray[80];
  ((String)g).toCharArray(charArray, 80);   // error: expected constructor, destructor, or type conversion before 'g'

  boolean stringComplete = false;  // whether the string is complete

void setup()
{
  delay(1000);
  Ethernet.begin(mac, ip);
  // Ethernet.begin(mac);
  Serial.begin(9600);
//  String::toCharArray()  //testing
  inputString.reserve(200); //(200
//  inputString.toCharArray(g, 80);
  if (twitter.post(charArray))
  {

Original comment by rmacar...@gmail.com on 24 Apr 2012 at 4:45

GoogleCodeExporter commented 9 years ago
This has been fixed in Arduino 1.0.

Original comment by matthew....@gmail.com on 24 Apr 2012 at 8:12

GoogleCodeExporter commented 9 years ago
Thank you!

how has it been fixed?
((String)g).toCharArray(charArray, 80)
gives errors no matter how i phrase it

Original comment by rmacar...@gmail.com on 24 Apr 2012 at 8:36