# String of number arrays

This is a two part question:

For the first part, I was looking at the String tutorial, and I want to do something similar as shown in this example:

``````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"};
``````

However, I want to instead of character strings, store number strings of different lengths:

``````byte* someStrings[]={ {0x1b,0x02,0x18,0x26}, {0x0e,0x15,0x00,0x09,0x29}, {0x06,0x21,0x29} };
``````

The above does not compile. Is it possible to do if coded properly?

Second question: My end goal is to have an array of strings made of numbers, then parse each number in a string to a function that communicates that number to PORTD. What is the most efficient way to do this?

For example:

``````#define printPORTD(p)    (PORTD = (PORTD & 3) | ((p) << 2))

byte* someStrings[]={ {0x1b,0x02,0x18,0x26}, {0x0e,0x15,0x00,0x09,0x29}, {0x06,0x21,0x29} };

int i = 1;

send each number in someStrings[i] to printPORTD(p)
``````

For your first question, I would do the initialization as:

``````  byte str1[] = {0x1b,0x02,0x18,0x26};
byte str2[] = {0x0e,0x15,0x00,0x09,0x29};
byte str3[] = {0x06,0x21,0x29};
byte* someStrings[] = {str1, str2, str3};
``````

Be aware that this is an array of arrays and not an array of null terminated strings. You can't treat them the same.

Maybe you can better explain what exactly you are trying to do.

(edited for typos.)

Montecito, thanks for your reply. I was hoping to not declare each string separately since I might end up with a few hundred. But if that is the only way it can be done.

As for the second part, I need a function that takes one of the strings, like let’s say someStrings[1]. Then it it sends sequentially each value within the string to printPORTD(p). The function could handle a string of any length of numbers (say up to 20). Maybe it has to be a for loop?

``````for (int i = 0; i < ***length of someStrings[1]***; i++){
printPORTD(someStrings[1][i]);
}
``````

printPORTD ends up receiving in sequence: 0x0e then 0x15, 0x00, 0x09, 0x29

The elements of someStrings[] are technically not "strings" they are arrays. strings are arrays of characters which are terminated by the NULL character and hence the length can be determined. (NULL is not a valid character and thus acts as a sentinel marking the end of the string).

The problem with an array of arbitrary bytes is that you don't know the length of each element in your array someStrings[] since you said you wanted each one to be of different lengths. For an array of bytes you have no such option as with a string as you probably cannot assume a byte value of 0x0 terminates the array. Unless you can allow 0x0 to terminate the byte arrays your options are:

1) use a two-dimensional array where the length of each row is fixed to known value

or

2) somehow store the length of each array of bytes in deterministic manner.

For option #2 you could store the number of elements in each byte array as the first element in the array. This gives the number of valid bytes that follow. While this will work it is messy to write and messy to maintain if the values in your byte arrays change. There are other methods such as maintaining an array of integers where each integer gives the length of the corresponding byte array or actually making your someStrings[] array be a pointer to a struct where the struct members are a byte count as well as the actual byte array.

I hope this helps.