andyz
andyz

Reputation: 55

What does this Union mean? c++

So I was given a Union like this:

union A
{
    struct{
        SomeType B;
    }b;
    struct{
        SomeType C;
    }c;
}a[2];

What does that a[2] mean?

Upvotes: 0

Views: 95

Answers (3)

NathanOliver
NathanOliver

Reputation: 180595

It is an array named a of size 2 of the type A which is a union. This is the same for all class types.

It would be the same as

union A
{
    struct{
        SomeType B;
    }b;
    struct{
        SomeType C;
    }c;
};

A a[2];

Upvotes: 6

Vlad from Moscow
Vlad from Moscow

Reputation: 310980

This declaration

union A
{
    struct{
        SomeType B
    }b;
    struct{
        SomeType C
    }c;
}a[2];

in fact is equivalent to the following two declarations

union A
{
    struct{
        SomeType B
    }b;
    struct{
        SomeType C
    }c;
};

A a[2];

That is a is an array of two elements of type union A.

Upvotes: 2

user2209008
user2209008

Reputation:

The a[2] is declaring a variable, a, that is an array of type A, that has a size of 2.

Upvotes: 0

Related Questions