Mike
Mike

Reputation: 4288

Which is the correct way to initialize a union/struct?

I have got the following union:

typedef union
{
    struct
    {
        uint8_t LSB;
        uint8_t MSB;
    };
    int16_t     complete;
}uint16ByteT;

Know I want to use my type and initialize the variable. After scanning SO ( I thought) I found the solution:

uint16ByteT   myVariable  = {0};

But my compiler gives me an Error message:

simple type required for "@"

Normally the xc8 compiler uses the "@" to bring a variable at a specific address.

Upvotes: 0

Views: 588

Answers (1)

David Ranieri
David Ranieri

Reputation: 41036

To initialize an anonym struct/union you can use:

uint16ByteT myVariable = {{0}, .complete = 0};

or simply

uint16ByteT myVariable = {{0}};

Notice uint16ByteT instead of uint16Byte

Also notice that you need to compile in C11 mode since anonym structs/unions was introduced in this version.

Upvotes: 1

Related Questions