Getting character array from a Library Tab

Hi all,

I have a sketch which uses a Real Time Clock to count down the time remaining until a certain date and display it on a 16*2 LCD. At the same time I made the info scroll from right to left on the display.

Everything works as expected, even the scrolling which was fun to do as it scrolls just one row leaving the other static.

However as I like to keep my code organized, after I finished the sketch on a sigle .ino file, I want to create separate libraries to handle LCD and RTC codes.

Firstly I created the RTC library and apparently works as intended. I created a public method that returns a char array. It looks like this:

char RtcSetup::timeChar(){

//some calculations

String preCharLib;
preCharLib=preCharLib + weeks + " Woche(n) " + days + " Tag(e) " + hours + " Stunde(n) " + minutes + " Minute(n)";

char secondLine[preCharLib.length()+1];  // create the char array

preCharLib.toCharArray(secondLine, preCharLib.length()+1);  // populate the array

return secondLine;

  }

The code compiles ok so far. The problem arises when I try to call this created char array from the .ino file

If I try to call it like this:

char secondLine[]=rtClock.timeChar();

I got the following error:

initializer fails to determine size of 'secondLine'

If I try to call it like this:

char secondLine=rtClock.timeChar();

and I try to do somethig like this

temporal[i]=secondLine[i+foo];

I got the error:

invalid types 'char[int]' for array subscript

Getting an integer of the length of the char array and using in when initializing on the .ino file, also does not work.

I know I could be much better off working with Strings instead, I guess. But for a future expansion, I really need to work with char array, which I never found easy to begin with.

I really tried google the issues, but I haven't found anything really helpful.
Any help in moving on, will be much appreciated.

Your function is declared as returning a single character. If you want to return a string (character array) make it return a character pointer:

char *RtcSetup::timeChar(){

You should also make the buffer "static char secondLine[" so that it still exists after the function returns. Returning a pointer to a local variable is not safe. I don't think you can create a static array to a variable size since the array is allocated before the sketch executes.

Do you really need to make a copy of the returned array into a local variable? You will probably need to store the returned pointer into a local pointer and use strlen() to get the length of the string so you can create a local array of the right length. THEN you can use strcpy() to move the string from the static array to your local array.

thanks @johnwasser for your explanation. I will start experimenting on the basis of your comments. I think I have it a little bit better idea on my mind now.