Questions tagged [unions]

The plural of a keyword in the C family of languages for declaring a union data type.

A union is a single variable with multiple possible representations. For example:

union foo {
    int a;
    float b;
    struct {
        char c, d;
    } e;
};

is a variable foo that can be accessed as either an int, float, or struct. Although similar in format to a struct, the size of a union is equal to the size of the largest field where as the size of a structure is the sum of the sizes of all fields.

They can also be used for a primitive form of polymorphism. A program can check for types and use different fields in a union in different contexts.


For the SQL UNION keyword, use instead of this tag.

1638 questions
464
votes
17 answers

Difference between a Structure and a Union

Is there any good example to give the difference between a struct and a union? Basically I know that struct uses all the memory of its member and union uses the largest members memory space. Is there any other OS level difference?
gagneet
  • 35,729
  • 29
  • 78
  • 113
328
votes
16 answers

Purpose of Unions in C and C++

I have used unions earlier comfortably; today I was alarmed when I read this post and came to know that this code union ARGB { uint32_t colour; struct componentsTag { uint8_t b; uint8_t g; uint8_t r; …
legends2k
  • 31,634
  • 25
  • 118
  • 222
303
votes
20 answers

Why do we need C Unions?

When should unions be used? Why do we need them?
ramu
196
votes
5 answers

Typescript has unions, so are enums redundant?

Ever since TypeScript introduced unions types, I wonder if there is any reason to declare an enum type. Consider the following enum type declaration: enum X { A, B, C } var x: X = X.A; and a similar union type declaration: type X: "A" | "B" |…
prmph
  • 7,616
  • 11
  • 37
  • 46
152
votes
18 answers

When would anyone use a union? Is it a remnant from the C-only days?

I have learned but don't really get unions. Every C or C++ text I go through introduces them (sometimes in passing), but they tend to give very few practical examples of why or where to use them. When would unions be useful in a modern (or even…
Russel
  • 2,335
  • 3
  • 19
  • 11
146
votes
5 answers

Accessing inactive union member and undefined behavior?

I was under the impression that accessing a union member other than the last one set is UB, but I can't seem to find a solid reference (other than answers claiming it's UB but without any support from the standard). So, is it undefined behavior?
Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
113
votes
5 answers

Unions and type-punning

I've been searching for a while, but can't find a clear answer. Lots of people say that using unions to type-pun is undefined and bad practice. Why is this? I can't see any reason why it would do anything undefined considering the memory you write…
Matthew Wilkins
  • 1,335
  • 2
  • 12
  • 12
108
votes
7 answers

Why does C++ disallow anonymous structs?

Some C++ compilers permit anonymous unions and structs as an extension to standard C++. It's a bit of syntactic sugar that's occasionally very helpful. What's the rationale that prevents this from being part of the standard? Is there a technical…
Adrian McCarthy
  • 45,555
  • 16
  • 123
  • 175
98
votes
2 answers

What's the purpose of using a union with only one member?

When I was reading seastar source code, I noticed that there is a union structure called tx_side which has only one member. Is this some hack to deal with a certain problem? FYI, I paste the tx_side structure below: union tx_side { tx_side() {} …
xiaoming-qxm
  • 1,738
  • 14
  • 23
94
votes
8 answers

What does "request for member '*******' in something not a structure or union" mean?

Is there an easy explanation for what this error means? request for member '*******' in something not a structure or union I've encountered it several times in the time that I've been learning C, but I haven't got a clue as to what it means.
Pieter
  • 31,619
  • 76
  • 167
  • 242
85
votes
7 answers

C++ union in C#

I'm translating a library written in C++ to C#, and the keyword 'union' exists once. In a struct. What's the correct way of translating it into C#? And what does it do? It looks something like this; struct Foo { float bar; union { …
Viktor Elofsson
  • 1,581
  • 2
  • 13
  • 20
70
votes
10 answers

How to compile C code with anonymous structs / unions?

I can do this in c++/g++: struct vec3 { union { struct { float x, y, z; }; float xyz[3]; }; }; Then, vec3 v; assert(&v.xyz[0] == &v.x); assert(&v.xyz[1] == &v.y); assert(&v.xyz[2] == &v.z); will…
solinent
  • 1,605
  • 1
  • 17
  • 19
70
votes
4 answers

Is type-punning through a union unspecified in C99, and has it become specified in C11?

A number of answers for the Stack Overflow question Getting the IEEE Single-precision bits for a float suggest using a union structure for type punning (e.g.: turning the bits of a float into a uint32_t): union { float f; uint32_t u; }…
sfstewman
  • 5,589
  • 1
  • 19
  • 26
68
votes
3 answers

How do boost::variant and boost::any work?

How do variant and any from the boost library work internally? In a project I am working on, I currently use a tagged union. I want to use something else, because unions in C++ don't let you use objects with constructors, destructors or overloaded…
salvador p
  • 2,905
  • 5
  • 26
  • 26
57
votes
7 answers

Do union types actually exist in python?

Since python is dynamically typed, of course we can do something like this: def f(x): return 2 if x else "s" But is this the way python was actually intended to be used? Or in other words, do union types exist in the sense they do in Racket for…
Lana
  • 1,124
  • 1
  • 8
  • 17
1
2 3
99 100