I have a bunch of 3D arrays in the following form:
byte lines[][4][2] {
{ {B00000000, B11110000 },
{B00000000, B11110000 },
{B00000000, B11110000 },
{B00000000, B11110000 } },
{ {B11110000, B00000111 },
{B00000000, B11111000 },
{B00000000, B11111000 },
{B00000000, B11111000 } },
// continues for numerous elements
}
They are a bunch of values to push out to a pair of shift registers controlling an LED matrix. Each array is essentially an animation that plays across the LED matrix.
I would like to put my collection of arrays into an array so I can iterate through them. I know I can just combine them into one big array but the code is already opaque and I would at least like to keep separate named arrays so I can sequence and rearrange a series of animations by rearranging a series of named arrays in another array rather than one massive array of opaque binary values.
I want to do something like this
sometype* sequence[] = { lines, circles, squares } ;
or if that's not possible then:
sometype* sequence[3];
sometype[0] = lines;
sometype[1] = circles;
sometype[2] = squares;
Can I do this or some approximation thereof? If so what is "sometype"? I can't figure out how to say "an array of pointers to 3d byte arrays" or whatever it is I am trying to do. What is the syntax for deeclaring the array and dereferencing the pointers?
2 Answers 2
Try this:
byte (*sequence[])[4][2] = { lines, circles, squares } ;
Explanation: The identifiers lines
, circles
and squares
decay
to pointers to the first element of each array. Their decayed types are
then "pointer to array of length 4 of array of length 2 of byte". This
type could be declared as
typedef byte (*pointer_to_2d_array)[4][2];
The sequence is then an array of this type, which can be declared as
pointer_to_2d_array sequence[] = { ... };
or, if you appreciate the obscure elegance of the C syntax, as in the beginning of this answer.
Yes, you can ( both ways )
You need
typedef byte sometype[4][2];
However, you'll probably need the size of lines, circles, etc. , which is lost by a pointer.
One way might be an end marker (A special element, indicating there are no more) Another might be a
struct sometype {
size_t n;
byte [][4][2] data;
} lines ;
IMO, even 2D arrays are reaching the limits of human intelligence, and I'd prefer even more user defined types:
typedef byte Pattern[4][2]; // or a more appropriate name
Pattern lineArray[] {
{ {B00000000, B11110000 },
{B00000000, B11110000 },
{B00000000, B11110000 },
{B00000000, B11110000 } }
, { {B11110000, B00000111 },
{B00000000, B11111000 },
{B00000000, B11111000 },
{B00000000, B11111000 } }
};
typedef struct {
size_t count;
Pattern* data;
} PatternCollection;
PatternCollection lines = {sizeof(lineArray)/sizeof(Pattern), lineArray };
PatternCollection sequence[] = {lines /* , circles, squares */ }; // if you ever need to iterate them