string array to char

Hi All,

I had some wonderful; help on here earlier and was hoping to pick some more brains...

currently, I have, I believe an array of Strings:

String line[50];
String linetime[50];

Line contains up to 50 strings of no more than 20 character
linetime also contains 50 strings of 20 characters

linetime is a string representation of the time something happened from an rtc module
line is what happened.
I do need to keep them separate though.

I set them as arrays so I could scroll through the events on an lcd 20x4 screen.

I have read though that Strings are not a good idea and Char is a better option.

I am using Char for single lines, but is there a way to use it as an array, as it is already a character arrary?

thanks in advance

That’s a lot of memory... what arduino do you have?

You can use char lines[50][21]; to have 50 lines of 20 chars (plus space for the null terminating marker of cString)

Yes, you can have multidimensional character arrays. They tend to waste space, but do not present the often-fatal problems that Strings do.

Example:

char s[3][6]={"one", "two", "three"};
String line[50];// 300 bytes, even empty
String linetime[50]; // another 300 bytes, even empty

Thanks all.

I am using a mega as I also use a lot of pins.

I had seen that Strings were bad for memory which is why I was trying to move away, so thanks for explaining how bad they are.

If I need to write or read from/to a char array, how would I do that?

To move C-strings (zero terminated character arrays) to/from a character array use the strncpy() function.

Better yet, explore the rich universe of <string.h>!

JonMiles:
Line contains up to 50 strings of no more than 20 character
linetime also contains 50 strings of 20 characters

linetime is a string representation of the time something happened from an rtc module
line is what happened.
I do need to keep them separate though.

That's a monumental waste of resources. You can store any point in time between 1 January 1970 and 19 January 2038 using only 4 bytes. And, that's with 1 second resolution. Google search for "Unix Time".

Also, you should learn about structs so that you can link the "something" with the time that it happens.

gfvalvo:
That's a monumental waste of resources. You can store any point in time between 1 January 1970 and 19 January 2038 using only 4 bytes. And, that's with 1 second resolution. Google search for "Unix Time".

I appreciate that it was a waste of resources, however the library I was using made no reference to unix time, which I routinely use for php, sql etc.

I will look for a library that allows it, as I appreciate its not efficient.

What RTC do you use?

its the DS1307.

I have found the adafruit library that will allow unix time, which will help with memory management.

Yes adafruit will do.
Note that this RTC is known to lose precision pretty quickly. A DS3231 would do much better

ah ok, thanks for the heads up.

silly question as I am connected to the network anyway, would I do better using ntp rather than a rtc?

sure - this way you get rid of one component and the time will always be accurate although slower to get (and as long as your internet connexion works).

J-M-L:
sure - this way you get rid of one component and the time will always be accurate although slower to get (and as long as your internet connexion works).

And, you don't need to go out to NTP every time you want to get the current time. Use the Time.h library. It uses millis() to keep time over the short term and you can true-up this value by pulling the current time from NTP every few minutes. That way, the call to get the current time is fast.