Printing char arrays

Hi,

Consider an array of chars stored in 4 lines by 20 chars each. This could be for example an array of chars to be printed on an LCD of 4x20.

char data[4][20];

 // next line prints one dimension of the array of characters from data[1][0] to data[1] [19]

Serial.print(data[1]);

// what if the array is dimensioned the other way round ie


char data[20][4];

// ie 20 chars in 4 lines

// How can you print  ( data[ ][1])  ?

Is there a way to print one whole line of the array other than using a loop?

One possible way

char data[2][6] =
{
  {'H', 'e', 'l', 'l', 'o', '\0'},
  {'W', 'o', 'r', 'l', 'd', '\0'}
};

void setup()
{
  Serial.begin(115200);
  while (!Serial);
  Serial.println(data[0]);
  Serial.println(data[1]);
}

void loop()
{
}

NOTE the extra column needed to hold the string terminator

Thanks for the reply UKHeliBob.

I now realise that what am asking is not possible...

Why the aversion to using a loop ?
Put the code in a function and call it when required

Perhaps I should have given more info on the situation.

I am using a array data[20][4] to hold the contents of data displayed on a 4 lines x 20 chars LCD.

The array is dimensioned as shown above. ie the column subscript first and the row second

I now want to print on the LCD one row, say row 1. That would be data[_][1]

Is there a way to print all 20 chars without a loop to print one by one?

I now understand that its not possible because those chars are not located in adjacent places in memory.

Correct ?

I now want to print on the LCD one row, say row 1. That would be data[_][1]

In a 2 dim array the first dimension is the row number, the second is the column number.

I now understand that its not possible because those chars are not located in adjacent places in memory.

Even if the data in the array were scattered throughout the memory (it’s not, which is why my example works) then using the correct array index values would return the correct values

Watcher:
I am using a array data[20][4] to hold the contents of data displayed on a 4 lines x 20 chars LCD.

Can you explain why you are storing the contents of a 4x20 display in a 20x4 array? Normally the array would be 4x21, to allow for the terminating null so you can use strings (null terminated char arrays), or an array of 4 char* .

It would be interesting to know how the array is being populated as that may have a bearing on the solution

david_2018:
Can you explain why you are storing the contents of a 4x20 display in a 20x4 array?

The only reason I chose 4x20 as opposed tp 20x4 is because I am used to having the 'x- coordinate' followed by the 'y-coordinate'

Normally the array would be 4x21, to allow for the terminating null so you can use strings (null terminated char arrays), or an array of 4 char* .

Correct. In fact the array is 4x22 to cater for an additional control character i need.

It would be interesting to know how the array is being populated as that may have a bearing on the solution

The system holds a menu who's items are not static.
The menu items are stored on an external EEPROM.
So depending to which submenu you are and on a set of other conditions, the actual menu items change and are loaded from the EEPROM to the array and then printed on the LCD.

I could give more info if interested...

In fact the array is 4x22 to cater for an additional control character i need.

There should be no problem printing it row by row then

Out of interest, how are you saving the array to EEPROM and loading it back in ? Please tell me that you are not using EEPROM.write() and EEPROM.read() but are using .put() and .get() instead

In fact the array is 4x22 to cater for an additional control character i need.

There should be no problem printing it row by row then

Sorry, my mistake. The array is 22x4!

Please tell me that you are not using EEPROM.write() and EEPROM.read() but are using .put() and .get() instead

I am using an external I2C EEPROM, not the MPUs on-chip EEPROM.

Define the char array as 4 x 22, then you only need to reference the first dimension with any function that takes a null-terminated char array. That way you can avoid the irritation of thinking of the array as having the x and y coordinates reversed. I would also think that using a 4x22 array would make it easier to code when declaring the contents for the eeprom, or at lease easier to read.

Yes. i now realise that.
Unfortunately its a bit too late as my program has progressed a lot by now and re defing the array is a major change.

i plan to switch to a color graphics TFT screen instead of an LCD one and i might make the change then.

Do you have any advice on data handling with tft screen as opposed to an LCD one?

Going back to your original question, what is your objection to using a for loop to print the characters ?

UKHeliBob:
Going back to your original question, what is your objection to using a for loop to print the characters ?

Thats what i am doing now.
I would have thought that printing a whole string would be faster.
This is a task that is executed continuously as the screen updates.

This is a task that is executed continuously as the screen updates.

I hope that you are only updating the screen when the data displayed on it changes. If not, then why not ?

You have been given the solution to your original question, ie rearrange your arrays, but you seem reluctant to do it

The screen is updated once every 500ms based on a timer interrupt.
I am thinking to change this and update it when something changes instead.
Guess i could keep track when update is needed with a bool variable..

This is a major change though and so is re-arranging the array.

Might do both when i switch to a tft screen...

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.