Reputation: 36536
I am using GCC 4.7.2 (C99) and Atmel Studio 6 for a microcontroller project.
I want to define a series of 1-dimensional arrays, then arrange these into various 2-dimensional arrays for predefined sequences.
For example:
unsigned char a1[2] = { 0b00000001, 0b00000010 };
unsigned char a2[2] = { 0b00000010, 0b00000010 };
unsigned char a3[2] = { 0b00000100, 0b00000010 };
unsigned char pattern1[1][2] = { a1, a2, a3 };
However, this results in the error:
Initializer element is not computable at load time
I can set up the array after compile time -
unsigned char pattern1[1][2];
void setup_patterns()
{
pattern1[0] = a1;
pattern1[1] = a2;
pattern1[2] = a3;
}
But it would be much easier to be able to provide a comma-separated list of the first set of arrays, given how many there are and how many patterns there will be. (The example shown here is very simplified.)
I'm not very experienced with C programming (I'm used to C#). Is there some way to assign the elements of a 2-dimensional array using a list of defined/named 1-dimensional arrays? (Whether at compile time or run time, it does not matter.)
Upvotes: 3
Views: 846
Reputation: 98108
Try this:
unsigned char *pattern1[] = { a1, a2, a3 };
the unsigned char pattern1[1][2]
requires:
unsigned char pattern1[1][2] = { {1, 1} };
so you can make your case compile by nested array initialization:
unsigned char pattern1[3][2] = {
{ 0b00000001, 0b00000010 },
{ 0b00000010, 0b00000010 },
{ 0b00000100, 0b00000010 },
};
Upvotes: 1