Skip to main content
Tweeted twitter.com/StackArduino/status/1136332226088591362
More specific title
Link
Edgar Bonet
  • 45.2k
  • 4
  • 42
  • 81

C++ Arduino IDE receiving Receiving garbled `char` fromarray returned by a function

Became Hot Network Question
Minor edit to code (indent "..." so it doesn't break the formatting)
Source Link
JRobert
  • 15.4k
  • 3
  • 25
  • 53

I return a char[300] array from a function. If I intialize a char* variable with it, the return is garbled, but not if I append it to a String. What gives?

  const char* post2 =  uploadHourCsv(timeNow,pulseChangeHour) ;
  String postS2 = "";
  postS2 =  uploadHourCsv(timeNow,pulseChangeHour) ;
  //const char* post2 = { uploadHourCsv(timeNow,pulseChangeHour) };
  if( debug ) {
    Serial.print("received in loop() as : [");
    Serial.println(post2);
    Serial.print("String : [");
    Serial.println(postS2);
  } 

...

char postStr[300] = "";
// populate array
if( debug ) {
  Serial.print("postStrCsv generated: ");
  Serial.println(postStr);
}
return postStr;

... char postStr[300] = ""; // populate array if( debug ) { Serial.print("postStrCsv generated: "); Serial.println(postStr); } return postStr;

postStrCsv generated: 00000003;3| 0.00;8|17.55;9|17.55;10|18.12;11|16.92;20|93817;22|93789;101|6;time|1559646000;

received in loop() as : [000?⸮@⸮⸮?" ⸮⸮?17.55;9|17.58! @0⸮⸮⸮⸮G% @5~`⸮h⸮?⸮⸮

String : [00000003;3| 0.00;8|17.55;9|17.55;10|18.12;11|16.92;20|93817;22|93789;101|6;time|1559646000;

I return a char[300] array from a function. If I intialize a char* variable with it, the return is garbled, but not if I append it to a String. What gives?

  const char* post2 =  uploadHourCsv(timeNow,pulseChangeHour) ;
  String postS2 = "";
  postS2 =  uploadHourCsv(timeNow,pulseChangeHour) ;
  //const char* post2 = { uploadHourCsv(timeNow,pulseChangeHour) };
  if( debug ) {
    Serial.print("received in loop() as : [");
    Serial.println(post2);
    Serial.print("String : [");
    Serial.println(postS2);
  }

... char postStr[300] = ""; // populate array if( debug ) { Serial.print("postStrCsv generated: "); Serial.println(postStr); } return postStr;

postStrCsv generated: 00000003;3| 0.00;8|17.55;9|17.55;10|18.12;11|16.92;20|93817;22|93789;101|6;time|1559646000;

received in loop() as : [000?⸮@⸮⸮?" ⸮⸮?17.55;9|17.58! @0⸮⸮⸮⸮G% @5~`⸮h⸮?⸮⸮

String : [00000003;3| 0.00;8|17.55;9|17.55;10|18.12;11|16.92;20|93817;22|93789;101|6;time|1559646000;

I return a char[300] array from a function. If I intialize a char* variable with it, the return is garbled, but not if I append it to a String. What gives?

  const char* post2 =  uploadHourCsv(timeNow,pulseChangeHour) ;
  String postS2 = "";
  postS2 =  uploadHourCsv(timeNow,pulseChangeHour) ;
  //const char* post2 = { uploadHourCsv(timeNow,pulseChangeHour) };
  if( debug ) {
    Serial.print("received in loop() as : [");
    Serial.println(post2);
    Serial.print("String : [");
    Serial.println(postS2);
  } 

...

char postStr[300] = "";
// populate array
if( debug ) {
  Serial.print("postStrCsv generated: ");
  Serial.println(postStr);
}
return postStr;

postStrCsv generated: 00000003;3| 0.00;8|17.55;9|17.55;10|18.12;11|16.92;20|93817;22|93789;101|6;time|1559646000;

received in loop() as : [000?⸮@⸮⸮?" ⸮⸮?17.55;9|17.58! @0⸮⸮⸮⸮G% @5~`⸮h⸮?⸮⸮

String : [00000003;3| 0.00;8|17.55;9|17.55;10|18.12;11|16.92;20|93817;22|93789;101|6;time|1559646000;

Source Link
tony gil
  • 388
  • 1
  • 7
  • 26

C++ Arduino IDE receiving garbled `char` from function

I return a char[300] array from a function. If I intialize a char* variable with it, the return is garbled, but not if I append it to a String. What gives?

  const char* post2 =  uploadHourCsv(timeNow,pulseChangeHour) ;
  String postS2 = "";
  postS2 =  uploadHourCsv(timeNow,pulseChangeHour) ;
  //const char* post2 = { uploadHourCsv(timeNow,pulseChangeHour) };
  if( debug ) {
    Serial.print("received in loop() as : [");
    Serial.println(post2);
    Serial.print("String : [");
    Serial.println(postS2);
  }

... char postStr[300] = ""; // populate array if( debug ) { Serial.print("postStrCsv generated: "); Serial.println(postStr); } return postStr;

postStrCsv generated: 00000003;3| 0.00;8|17.55;9|17.55;10|18.12;11|16.92;20|93817;22|93789;101|6;time|1559646000;

received in loop() as : [000?⸮@⸮⸮?" ⸮⸮?17.55;9|17.58! @0⸮⸮⸮⸮G% @5~`⸮h⸮?⸮⸮

String : [00000003;3| 0.00;8|17.55;9|17.55;10|18.12;11|16.92;20|93817;22|93789;101|6;time|1559646000;