Salahuddin
Salahuddin

Reputation: 1719

Initialize a constant array of struct with bitfield

I'd like to initialize a const array of structs. These structs have bitfield members.

Following is a snippet of my code:

typedef struct {
    unsigned int a : 1;
    unsigned int b : 1;
    unsigned int c : 1;
} Character;

const static Character Char[] =
{
    {.a = 0, .b = 0, .c = 1},
    {.a = 0, .b = 1, .c = 0},
    {.a = 1, .b = 0, .c = 1}
};

When trying this way, I got many errors like unexpected initialization syntax and missing ;.

What is the right way to do this?

UPDATE

I'm using COSMIC compiler (CXSTM8). I checked its user guide but couldn't find any information in this regard.

Upvotes: 1

Views: 576

Answers (1)

Rishikesh Raje
Rishikesh Raje

Reputation: 8614

The syntax you have given is correct. The designated initializer list was introduced in C99.

If your compiler does not support this, you need to go for the next best option. i.e. initialize all the members in the bitfield.

typedef struct {
    unsigned int a : 1;
    unsigned int b : 1;
    unsigned int c : 1;
} Character;

const static Character Char[] =
{
    {0, 0, 1},
    {0, 1, 0},
    {1, 0, 1}
}; 

Upvotes: 3

Related Questions