user81993
user81993

Reputation: 6613

Declaring union typed variables

I'm exploring some ancient C library and am a bit confused as to why it declares union variables in a certain way and whether or not it makes any difference, example:

union banana {
    uint32_t cool[2];
    uint64_t supercool;
};

void main() {
    union banana new_banana; //<- why like this?
    banana other_banana; // <- as opposed to this
}

As far as I can tell it doesn't make any difference but you never know.. is there any?

Upvotes: 0

Views: 62

Answers (1)

Manny_Mar
Manny_Mar

Reputation: 398

Unless you use typedef you have to use the union in front of banana.

typedef union {
    uint32_t cool[2];
    uint64_t supercool;
} banana;

Then you can just use banana. If you just do as in the example above, then you will have to use union banana, and you can even use banana for something else. union banana and banana are different in the example above.

if you try banana other_bananaas in your example, the compiler should give you error: unknown type name ‘banana’

Upvotes: 1

Related Questions