Arduino array of strings (char arrays)

Thread Starter

djsfantasi

Joined Apr 11, 2010
9,163
I'm trying to figure out char arrays on the Arduino. I have a two dimensional array containing messages that I want to send out the serial port. One entry looks like:

Rich (BB code):
MoveCommand[5][20]="#0P1500T3000"
And to send it to the serial port, would the syntax be:

Rich (BB code):
serial.println MoveCommand[5][];
:confused:?

I don't have the Arduino yet to test, so I'm asking here
 

ErnieM

Joined Apr 24, 2011
8,377
Three basic things: If your array (or string) is not a constant array then it must be stored twice, once in ROM and once in RAM. Making it constant takes out the RAM requirement, but you may need to copy it first to a RAM array to use it.

Next, unless all of your 5 times 20 = 100 arrays are the same length you may again wasting space. So if they are of differing lengths make the array not of strings but of pointers to strings. Depending on the data that may be smaller. (The tradeoff is you need an additional 100 pointer locations minus the max string length differences.)

Last, if the strings are all the same length, don't store as strings and you'll save 100 char locations just used to hold the trailing zero of each string.
 

Thread Starter

djsfantasi

Joined Apr 11, 2010
9,163
The references only show examples with one dimensional arrays.

However. Since you have multiple messages (strings), you can do array of strings. See the last example on this page:
http://arduino.cc/en/Reference/String
The array is defined as [64][20], so isn't that two dimensional?

Is the serial.println example correct for printing the fifth entry of the above array?
 
Last edited:

Thread Starter

djsfantasi

Joined Apr 11, 2010
9,163
Ernie,

The array is stored as constants.

Thanks for the tips on conserving memory. It isn't an issue now, but they will be helpful in the future.
 

ErnieM

Joined Apr 24, 2011
8,377
Ernie,

The array is stored as constants.

Thanks for the tips on conserving memory. It isn't an issue now, but they will be helpful in the future.
If the array is constant then how can
Rich (BB code):
MoveCommand[5][20]="#0P1500T3000"
not generate an error? Note I do not use the Arduino, I am interpeting this as C code.

char ch[64][20]

is two dimensional array of characters, i.e. 64 strings of 20 characters.
And that has room for 64 strings of 19 characters each, assuming a string needs the trailing zero (see my note above).
 

sirch2

Joined Jan 21, 2013
1,037
off the top of my head is it not more like this

Rich (BB code):
char ch[64][21]; //extra char for string terminator
strcpy(ch[0], "blah"); 
strcpy(ch[1], "blah blah");

serial.println(ch[0]);
Although as others have said if the Strings are constants there are probably more memory efficient ways of handling this.
 

Thread Starter

djsfantasi

Joined Apr 11, 2010
9,163
Ok, I was misleading in my initial post. In my example of one entry, I wasn't illustrating assignment; I meant "MoveCommand[5][20] has the value of "#0P1500T3000"

My actual code looks like this extract...
Rich (BB code):
const char* MoveCommand[][60]={
"#0P1500#1P1500#2P1000#3P570#4P1500#5P2400#6P510#8P1500#9P1500#10P1500#11P1400T1000",
"#2P1100",
"#0P2000*#1P1100*T100",
"#2P750",
etc... for up to 60 entries
}
So to print oout the ith entry, is the syntax:
Rich (BB code):
serial.println MoveCommand[];

:confused:
 

ErnieM

Joined Apr 24, 2011
8,377
Rich (BB code):
const char* MoveCommand[][60]={
"#0P1500#1P1500#2P1000#3P570#4P1500#5P2400#6P510#8P1500#9P1500#10P1500#11P1400T1000",
"#2P1100",
"#0P2000*#1P1100*T100",
"#2P750",
etc... for up to 60 entries
}
If you have 60 strings of variable length you need a vector (a one dimensional array) of 60 pointers to see them all. It is a vector as the length of the referenced object (the string) is irrlevant as to where the string is.

Yes the length is importaint as the lengths of the strings listed are 82, 7, 21 and 7 chars respectively, and you only wish to print that many each time. By using the quotes in the definition the Arduino compiler puts that zero in for you so these strings will work with functions such as serial.println

I do note your 1st string exceeds your expectation of 60 chars max. But no bother as you have an array of pointers so the 2nd dimension is unnecessary and also incorrect.

Something like this would be preferable:
Rich (BB code):
const char* MoveCommand[]={
"#0P1500#1P1500#2P1000#3P570#4P1500#5P2400#6P510#8P1500#9P1500#10P1500#11P1400T1000",
"#2P1100",
"#0P2000*#1P1100*T100",
"#2P750",
etc... for up to 60 entries
}
See example #2 here

And to print this out:
Rich (BB code):
serial.println MoveCommand;


Again, DO NOTE I have never programmed this machine. I just read a user's guide and apply my knowedge of C.
 

shteii01

Joined Feb 19, 2010
4,644
The array is defined as [64][20], so isn't that two dimensional?

Is the serial.println example correct for printing the fifth entry of the above array?
I am talking about Arduino. All the Arduino examples I have looked have one dimensional arrays. I am not Arduino guru so I don't know all the ins and outs of Arduino arrays, but, at this point in time, I have a feeling that Arduino only support one dimensional arrays.
So.
You can do:
* try using two dimensional array when you get the board and find out if they work
* e-mail Arduino software development team and ask them if their software support arrays with more than one dimension
* ask on Arduino forums, there must be people who already tried it
 

Thread Starter

djsfantasi

Joined Apr 11, 2010
9,163
The code compiles fine. Comments from the Arduino forum led me to this solution. I asked here because I know of members who are Arduino experts. My confusion stems from referencing the individual strings/messages.

I can't order the board right now, as I'm unemployed but have been coding in the IDE.
 
Last edited:

Thread Starter

djsfantasi

Joined Apr 11, 2010
9,163
So I successfully downloaded a trial Arduino simulator and wrote a test sketch. Thank you, ErnieM. Your comments were right on. I just was so dense, I couldn't see it until I had written code which could be executed.

As I understand it, MoveCommand is an array of pointers, which in turn points to my messages. There is no need for the second dimension. Defining them as constant keeps them in flash memory, leaving SRAM available for dynamic data structures and variables.

Having said that, to print out the ith message, I'd code:
Rich (BB code):
Serial.println(MoveCommand);
 

Thread Starter

djsfantasi

Joined Apr 11, 2010
9,163
Update: I almost had it. Defining them as constant didn't put them into program memory :confused: There is an Arduino library, PGMSPACE, that extends data types so they can be stored in program space. The table cannot be used directly (it's rows have to be copied from program space into RAM when needed) but as in my program, there is only one place the tables values are needed, it is easy to use!

I ran into another problem where I am assigning values to a table (the code is automatically generated in an external program). It was to a set of global integer arrays. I learned that definition and initialization can occur in the global definition section, but that assignment must take place in a function. I did it in setup. No thanks to the compiler error messages!

Thanks to all and a special thanks to ErnieM.
 
Top