My project needed an “Array of Strings” to simplify the code, allowing me to manipulate several strings at once using “for” loops, instead of having to type a separate line for each string I want to read, write, or edit.
But when I googled for info on [arduino “array of strings”], I was astonished to find each reference taking me instead to examples of character arrays being loaded with constants, such as:
char* myStrings={“This is string 1”, “This is string 2”, “This is string 3”,
“This is string 4”, “This is string 5”,“This is string 6”};
That won’t work for me at all; because, (1) Constants are constants, “hard wired” so to speak, not something you’re sketch can change as needed. And (2) they are going into char arrays, not Strings, which means Adruino’s rich list of String-manipulation functions won’t work on them.
One reason for using something so “carved in stone”, is that they use much less of Arduino’s precious memory. And this is fine if you don’t plan to manipulate them further within your program.
A second reason is that a true “Array of Strings” requires using pointers; and so many people find the subject of pointers rather mind-boggling, even frightening.
Well, I look at it this way: A pointer is simply the address where an object in your program lives, no different from the address where I live. The mind-boggling part is that if I simply told someone “801 Main St.”, they’d have no idea what I was talking about. It could be the location of a house, a grocery, a fire station, a water park, or most anything else. And even worse, “801 Main St.” doesn’t even tell them how far or in which direction!
But three tools in the Arduino language make using pointers a lot simpler than it might at first seem: (1) The ‘&’ symbol is like writing the address down on a piece of paper and handing it to someone through the ‘=’ symbol. (2) Even better yet, the ‘*’ symbol actually takes you to that address, where you can knock on the door, repaint the front porch, or whatever you have in mind. (No road-map required.)
And finally, (3) you’d probably like to know WHAT you’re being taken to before you hitch a ride, and not wind up at the city dump after expecting a fancy restaurant. Arduino takes care of this too, by naming the kind of pointer before you use it. For example, the word “String” in the line, “String * fString[8];” below is the promise that when later used with “fString”, the ‘*’ symbol will take you to a “String” (not the city dump, or even a “char” array). And a promise is a promise! If you try to give it the address to something else, your sketch won’t compile; and an error message will inform you that was NOT a string!
So really, pointers aren’t that difficult at all. You just need to write down the address where you want to go (using ‘&’), ask for sure if it’s what you think (in this case, “String”), and then hop aboard the ‘*’ bus to enjoy the ride all the way there.
Here’s a little sketch to demonstrate using a real “Array of Strings”, (and of course, our friend, the pointer).
// To see the result of this sketch after loading it, open the
// serial monitor and then press the reset button.
String str = "^John Smith^Susie Maye^Tom Jones^Sarrah Jones^"
"Martha Mayes^Brenda Howard^Dan Evens^";
String s0,s1,s2,s3,s4,s5,s6;// <-- Create real strings
String * fString[8];// <-- The '*' says this will be an array of pointers,
// while the word 'String' says these pointers will
// point to real Strings.
int strIndex = 0;
String splitStr()
{
if (strIndex == -1)
return "";
byte i = str.indexOf('^',strIndex)+1;
strIndex = str.indexOf('^',i);
if (strIndex == -1)
return "";
else
return str.substring(i,strIndex);
}
void setup()
{
Serial.begin(9600);
while (!Serial) {};
fString[0] = & s0;// <-- The '&' symbol sends just the String's pointer
fString[1] = & s1;// through the '=' symbol to the array.
fString[2] = & s2;//
fString[3] = & s3;// <-- We still must load the real Strings into the
fString[4] = & s4;// array, one by one.
fString[5] = & s5;//
fString[6] = & s6;//
}
void loop()
{
byte i;
for (i=0; i<7; i++)// <-- Now we can use the "Array of Strings" to
*fString[i] = splitStr();// make life simple, using "for" loops!
Serial.println(str);
Serial.println();
// Although I only printed these strings below, you can use
// any of the many String functions on these "*fString[n]"
// Strings. Available string functions are listed here:
// http://www.arduino.cc/en/Reference/StringObject
for (i=0; i<7; i++)
Serial.println(*fString[i]);
while (true) {};
}