Go Down

Topic: PIs it possible to use a String as a return value? (Read 538 times) previous topic - next topic

teddyz

If this is valid:
Code: [Select]

long timeNow ()
  {
        // Calculate time...
        return timeValue // timeValue as long
  }


Why can't I do this?
Code: [Select]

String timeNow ()
  {
        // Calculate time...
        return timeValue // timeValue as a String
  }


Which I could use like this:
Code: [Select]

  serial.println( timeNow() ):


Now I have declared a globally available static String called "returnString" that I use. It works, but I guess it would look better and be easier to use if this would be possible. Or have I missed something?

Thanks in advance!

Nick Gammon

How about posting code that has a chance of compiling? You have missing semicolons, a colon instead of a semicolon, and Serial as serial.

This compiles:

Code: [Select]

String timeNow ()
  {
  String timeValue = "foo";
  // Calculate time...
  return timeValue; // timeValue as a String
  }
 
  void setup ()
  {
  Serial.begin (115200);
  Serial.println( timeNow() );
  }  // end of setup

void loop () { }


Please note that, at present, the String library has bugs as discussed here and here.

In particular, the dynamic memory allocation used by the String class may fail and cause random crashes.

I recommend reworking your code to manage without String. Use C-style strings instead (strcpy, strcat, strcmp, etc.).

Alternatively, install the fix described here:
  http://arduino.cc/forum/index.php/topic,145765
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

GoForSmoke

You'd be better off taking the address of the (ugg!) String as an arg and modify that. Return an error code or void or something.

To add 1 char to a String, it makes a copy of itself and wipes the original which is smaller. Add another char and guess what? It won't fit in the cleared spot you get if you use the String fix. But since that erases the one it was copied from (to add 1 char) your next add a char can copy back in the now empty space. And where does the next add 1 char operation copy to? Just guess!

Strings are just Heaps of Fun.



Nick Gammon on multitasking Arduinos:
1) http://gammon.com.au/blink
2) http://gammon.com.au/serial
3) http://gammon.com.au/interrupts

teddyz

Thanks for your replies. I managed to make String work, but it is not stable, with or without the fix.

The documentation does not mention strcpy and it's friends, so I assumed they did not exist. Next programming session will be about replacing String. It feels a bit sad having to replace these:

Code: [Select]

String decodeURL( String in ) { // Data from GET
  in.replace("%20", " ");
  in.replace("%C3%A5", "å");
  in.replace("%C3%A4", "ä");
  in.replace("%C3%B6", "ö");
  in.replace("%C3%85", "Å");
  in.replace("%C3%84", "Ä");
  in.replace("%C3%96", "Ö");
  return( in );
}


String SWE( String in ) { // Translate characters to the web client
  in.replace("å", "å");
  in.replace("ä", "ä");
  in.replace("ö", "&oring;");
  in.replace("Å", "Å");
  in.replace("Ä", "Ä");
  in.replace("Ö", "&Oring;");
  return( in );
}

GoForSmoke

You have to go to the roots of the C we use to get the good stuff.

AVR LibC
Quote

Modules
Here is a list of all modules:

http://www.nongnu.org/avr-libc/user-manual/modules.html

strings:
http://www.nongnu.org/avr-libc/user-manual/group__avr__string.html

There's piles of good tutorials on C strings.

The string functions like strncpy() have a pattern, the n is count and they don't add a terminating zero. That cuts the number of mnemonic parts you have to recognize/remember.

Nick Gammon on multitasking Arduinos:
1) http://gammon.com.au/blink
2) http://gammon.com.au/serial
3) http://gammon.com.au/interrupts

teddyz

Now I have learned something. There is a reason they made String! :-)

Okay, my big problem was Ethernetshield-related. I tried another and it worked fine, but at that point had I already started migrating from String.

I need help with this:

I converted my SWE-function (see above) to live without String. Now it looks like this, and relies on a replace-function found with Google.
Code: [Select]
void SWE( char * in ) { // Translate characters to the web client
  replace(in, "å", "å");
  replace(in, "ä", "ä");
  replace(in, "ö", "&oring;");
  replace(in, "Å", "Å");
  replace(in, "Ä", "Ä");
  replace(in, "Ö", "&Oring;");
  replace(in, "º", "°");
  replace(in, "°", "°");
}


Using it, I can convert char arrays. Great if they are variables.
But I can't convert this:
Code: [Select]
client.println("</br>");

To get around this problem I tried to put client.println() into the SWE-function, but it did not work.
Then I tried to use a pointer to it, but I can not make it compile "C.println(in);". I get the error "request for member 'println' in 'C', which is of non-class type 'EthernetClient*' ":

Code: [Select]
..
//Where I send html to the client
SWE("</br>", recTemp);
..


void SWE( char * in, EthernetClient * C ) { // Translate characters to the web client
  replace(in, "å", "&aring;");
  replace(in, "ä", "&auml;");
  replace(in, "ö", "&oring;");
  replace(in, "Å", "&Aring;");
  replace(in, "Ä", "&Auml;");
  replace(in, "Ö", "&Oring;");
  replace(in, "º", "&deg;");
  replace(in, "°", "&deg;");
  C.println(in);
}


Any ideas? Later, I would like to move the HTML to an SD card. That must have been done before? Perhaps with some functionality to put in variable values?

Go Up