Reputation: 43
Creating this struct:
typedef enum {
IDLE = 0,
SENT = 1,
RECEPTION = 2,
DONE = 3,
ABORT = 4,
} State_t;
typedef struct {
State_t state;
u8 N;
u8 last;
u16 time;
}__attribute__((packed)) Context_t;
Want to initialize it with struct variable names. This gives me an error:
static Context_t arr[MAX_CONTEXT] = {
.state = IDLE, .N= 1, .last= 0, .time= 0
};
as well as this:
static Context_t arr[MAX_CONTEXT];
arr = {
.state = IDLE, .N= 1, .last= 0, .time= 0
};
This of course work, but is not what i want:
static Context_t arr[MAX_CONTEXT] = {
IDLE, 1, 0, 0
};
Does anybody have a better way of implementing this struct?
Upvotes: 0
Views: 450
Reputation: 213950
You get errors because it usually doesn't make sense to initialize an array of struct with a single struct item's initializers.
If your intention is to only initialize the first item in the array, then it should be:
static Context_t arr [MAX_CONTEXT] =
{
{ .state = IDLE, .N= 1, .last= 0, .time= 0 },
};
or if you will: [0] = { .state = IDLE, .N= 1, .last= 0, .time= 0 },
Upvotes: 3
Reputation: 46
Depends on the compiler but try this one out maybe:
static Context_t arr[MAX_CONTEXT] = {
{.state=IDLE, .N=1, .last=0, .time=0}, // 0
{.state=IDLE, .N=1, .last=0, .time=0}, // 1
...
{.state=IDLE, .N=1, .last=0, .time=0} // MAX_CONTENT
};
Upvotes: 0