aswine
January 5, 2016, 6:58pm
1
I have several strings stored in char arrays. I'd like to to some things like:
char string0[10];
char string1[10];
char string2[10];
char string3[10];
char string4[10];
//somehow make an array or something of those strings called arrayOfStrings
for (int i; i<5; i++)
{
Serial.print(arrayOfStrings[i]);
}
If I were using Strings, I'd do:
String string0;
String string1;
String string2;
String string3;
String string4;
String* arrayOfStrings[5] =
{
&string0;
&string1;
&string2;
&string3;
&string4;
}
for (int i; i<5; i++)
{
Serial.print(arrayOfStrings[i]);
}
...but I don't know how to do that with char arrays. How can I do that?
Do you know how to declare an 2 dimensional array ?
aswine
January 5, 2016, 7:08pm
3
Yes, I've tried
char* arrayOfStrings[10][5] =
{
&string0[10];
&string1[10];
&string2[10];
&string3[10];
&string4[10];
}
but something about that didn't work, I don't remember why right now. Is that what you're thinking?
&stringX[10] does not exist when you declare a 10 element array. Try &stringX[0], a pointer to the first element of stringX, which is what you want, anyway.
guix
January 5, 2016, 7:27pm
5
KeithRB:
Try &stringX[0], a pointer to the first element of stringX
Which is the same thing as just stringX
Here are two solutions:
This one will waste some memory
char strings[][10] = // 10 is the length of the longest string + 1 ( for the '\0' at the end )
{
"hello",
"bonjour",
"guten tag"
};
And this one is less pleasant to look at
char string1[] = "hello";
char string2[] = "bonjour";
char string3[] = "guten tag";
char * strings[] =
{
string1,
string2,
string3
};
1 Like
Actually it is not the same thing and you are more correct. &StringX[0] is a pointer to a char, and StringX is a pointer to an array.
aswine
January 5, 2016, 7:55pm
7
Thanks, guix. That second one got me where I needed to go.