0

I want to create an array of array of struct. Where is the error here? Practically I have 2 leds with 4 states (patterns) with 3 parameters (a,b,c). Thank you in advance.

typedef struct {
    int a;
    int b;
    int c;
}CfgType;


CfgType Led[4][2];


Led[4][0]=
{   
    /*    Pattern         a           b      c  */
/*00*/    /*STATE0*/ {    5    ,    100 ,    2  },
/*01*/    /*STATE1*/ {    5    ,    100 ,    1  },                                                                                                                                                                                                                                                                  
/*02*/    /*STATE2*/ {   100   ,    100 ,    0  },                                                                                                                  
/*03*/    /*STATE3*/ {   100   ,    100 ,    0  },                                                                                                                                          
};


Led[4][1]=
{   
    /*    Pattern         a           b      c  */
/*00*/    /*STATE0*/ {    5    ,    100 ,    2  },
/*01*/    /*STATE1*/ {    5    ,    100 ,    1  },                                                                                                                                                                                                                                                                  
/*02*/    /*STATE2*/ {   100   ,    100 ,    0  },                                                                                                                  
/*03*/    /*STATE3*/ {   100   ,    100 ,    0  },                                                                                                                                          
};
2
  • 1
    You cannot assign anything to an array. Try to intitialize it with the values instead. As you don't have some complete snippet it cannot be seen if these parts are located close together (i.e. assignmen right next to definition) or in some separate places. Commented Dec 4, 2021 at 9:47
  • you can think of the array as having 4 rows and 2 columns. The rows are at offsets 0, 1, 2 and 3. Led[4] is offset 4 and this is outside the array. Commented Dec 4, 2021 at 9:50

2 Answers 2

2

You cannot assign anything to an array. Try to intitialize it with the values instead.

Besides that element Led[4] does not exist. Maximum index is 3.

Also you have switched the dimensions of your arrays. You seem to want to have 2 elements containing 4 elements of your struct.

This would look as follows:

typedef struct {
    int a;
    int b;
    int c;
} CfgType;


CfgType Led[2][4] =
{
[0] = {
    [0] = { .a =   5, .b = 100, .c = 2 },
    [1] = { .a =   5, .b = 100, .c = 1 },
    [2] = { .a = 100, .b = 100, .c = 0 },
    [3] = { .a = 100, .b = 100, .c = 0 }
      },
[1] = {
    [0] = { .a =   5, .b = 100, .c = 2 },
    [1] = { .a =   5, .b = 100, .c = 1 },
    [2] = { .a = 100, .b = 100, .c = 0 },
    [3] = { .a = 100, .b = 100, .c = 0 }
      }
};

This example uses designated initializers that makes it robust against adding new values in between or reorder

Sign up to request clarification or add additional context in comments.

4 Comments

I did not know about the designated initializers syntax. Thanks for sharing, cool!
They were introduced with C99 and I think they are really a good thing.
It makes the code a bit more verbose but I think they're great, because it's harder to shoot yourself in the foot when you're explicit.
True, you don't really need to use it for the array index but for struct members they are worth the extra typing.
1

First, swap the array sizes in the declaration. They should be reversed. Then, you can skip the assignments and initialize the array directly like so:

typedef struct {
    int a;
    int b;
    int c;
}CfgType;

CfgType Led[2][4] = {
    {   
        /*    Pattern         a           b      c  */
    /*00*/    /*STATE0*/ {    5    ,    100 ,    2  },
    /*01*/    /*STATE1*/ {    5    ,    100 ,    1  },                                                                                                                                                                                                                                        >
    /*02*/    /*STATE2*/ {   100   ,    100 ,    0  },
    /*03*/    /*STATE3*/ {   100   ,    100 ,    0  },
    },
    
    {   
        /*    Pattern         a           b      c  */
    /*00*/    /*STATE0*/ {    5    ,    100 ,    2  },
    /*01*/    /*STATE1*/ {    5    ,    100 ,    1  },                                                                                                                                                                                                                                        >
    /*02*/    /*STATE2*/ {   100   ,    100 ,    0  },
    /*03*/    /*STATE3*/ {   100   ,    100 ,    0  },
    }
};

I compiled with gcc and it works fine.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.