Making an array of arrays

h4t:
... can I do the following?

Why not try it? In the time that it takes to post a request and wait for a (hopefully) helpful response, couldn't you just try it?

I respectfully suggest that, in addition to the learning experience of trying to debug buggy code, you might find it more time-effective than posting a request and waiting for a response (which may or may not be helpful).

I mean, it's always OK to ask (reallly---as far as I am concerned, it is always OK), and if you need help, then I think the following might be more time-effective:

  1. Post the code. The complete sketch that you tried, not just the part that you think is giving problems. If it's part of a big project, boil things down to a small (but complete) sketch that shows the problem behavior.

  2. If it gave a compiler error message that you didn't understand, then post the message. Paste the complete message into your post (don't paraphrase).

  3. If it compiled but didn't give the results you expect, the tell us exactly what you got and what you don't understand about the difference between what you got and what you expected.

Anyhow...

In your code, patterns is an array of pointers to byte. Each element of the code is the address of the first element in an array of bytes. That's OK. (See Footnote.)

const byte* patterns[] = { pattern1, pattern2};

patterns[0] is a pointer to byte. Its value is the address of the first element in the pattern1 array.
patterns[1] is a pointer to byte. Its value is the address of the first element in the pattern2 array.
So...

The following can't possibly work because it is trying to initialize a byte variable with the value of a pointer.

  const byte currentPattern = &patterns[0];

You can, however, define a pointer to byte and initialize its value to be equal to the value of patterns[0] by the following:

    const byte * currentPattern = patterns[0];

Now, currentPattern is pointing to the first element of the pattern1 array and currentPattern[2] will refer to element number 2 of that array, which is pattern1[2].

Finally, note that, if you give Serial.print a char or a byte to print,it will display the ASCII character corresponding to the value of its argument. I don't know (or care) what ASCII character corresponds to a decimal value of 22, but I am really, really sure that Serial.print(currentPattern[2] won't print 22

If you want to print the decimal value of currentPattern1[2], then you can tell Serial.print what you have in mind:

Serial.print(currentPattern[2], DEC);

Regards,

Dave

Footnote:
I know that some people think my posts are too wordy, especially when I repeat myself, but sometimes I just can't help it. Really. It's a curse.

So---here goes:

In C and C++ if x is a pointer data type and n is an integer data type, the following two notations are treated exactly the same by the compiler

p[i]

Treated exactly the same as

*(p + i)

Exactly the same. Not "pretty much the same." Not "essentially the same." Exactly. Note that I am not saying that pointers are the same as arrays. Pointer variables positively, absolutely, unequivocally are not the same as arrays. That statement is incontrovertible. (And I am unanimous in that.) It's just notation.

Finally, it is important to remember that the name of an array is a const pointer to the data type of the declared array. The value of the pointer is equal to the address of the first element of the array.

Therefore, as far as the compiler is concerned, the following two statements are identical in the eyes of the compiler

const byte* patterns[] = {pattern1, pattern2};

Same as

const byte * patterns[] = {&pattern1[0], &pattern2[0]};

Many C and C++ programmers of my acquaintance seem to prefer the first form (less typing and less visual clutter, don'tcha know), but they mean exactly the same thing. Use whatever form seems appropriate to your context.