Pages: [1]   Go Down
Author Topic: PIs it possible to use a String as a return value?  (Read 488 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 2
Posts: 37
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If this is valid:
Code:
long timeNow ()
  {
        // Calculate time...
        return timeValue // timeValue as long
  }

Why can't I do this?
Code:
String timeNow ()
  {
        // Calculate time...
        return timeValue // timeValue as a String
  }

Which I could use like this:
Code:
  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!
Logged

Global Moderator
Online Online
Brattain Member
*****
Karma: 502
Posts: 19080
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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


Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 98
Posts: 4813
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.



Logged

I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

Offline Offline
Newbie
*
Karma: 2
Posts: 37
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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 );
}
Logged

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 98
Posts: 4813
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Logged

I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

Offline Offline
Newbie
*
Karma: 2
Posts: 37
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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:
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:
..
//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?
Logged

Pages: [1]   Go Up
Jump to: