StuHooper:
If I want to split a 140 char array into chars of 21 length for display on an LCD screen, what command/s do I need to do so?
Memcpy is your friend:
#include <string.h>
static const int lcd_width = 20;
void print_large_array (const char *array, size_t len)
{
char small_array[lcd_width+1];
if (len == 0)
len = strlen (array); // figure out length if not passed
while (len > 0) {
// only print 20 characters per line
size_t len2 = (len > lcd_width) ? lcd_width : len;
memcpy ((void *)&small_array[0], (void *)array, len2);
small_array[len2] = '\0'; // add terminating null for println
lcd.println (small_array);
array += len2; // reset pointer/len for next iteration
len -= len2;
}
}
Note, that memcpy assumes the source and destination do not overlap. If they do overlap, you should use memmove instead.
Alternatively if you are using the raw array and not a pointer to it, you could do it as:
#include <string.h>
static const int lcd_width = 20;
void print_large_array (const char *array, size_t len)
{
char small_array[lcd_width+1];
size_t start = 0;
if (len == 0)
len = strlen (array); // figure out length if not passed
while (len > 0) {
// only print 20 characters per line
size_t len2 = (len > lcd_width) ? lcd_width : len;
memcpy ((void *)&small_array[0], (void *)&array[start], len2);
small_array[len2] = '\0'; // add terminating null for println
lcd.println (small_array);
start += len2; // reset pointer/len for next iteration
len -= len2;
}
}
This program logically does:
static const int lcd_width = 20;
void print_large_array (const char *array, size_t len)
{
size_t i;
char small_array[21];
if (len == 0)
len = strlen (array); // figure out length if not passed
while (len > 0) {
size_t len2 = (len > lcd_width) ? lcd_width : len;
for (i = 0; i < len2; i++)
small_array[i] = array[i];
small_array[len2] = '\0';
lcd.println (small_array);
array += len2; // reset pointer/len for next iteration
len -= len2;
}
}