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
15
votes
5 answers

Union and Struct Initialization

I stumbled across a code based on unions in C. Here is the code: union { struct { char ax[2]; char ab[2]; } s; struct { int a; int b; } st; …
h4ck3d
  • 6,134
  • 15
  • 51
  • 74
15
votes
4 answers

Union with anonymous struct with flexible array member

Consider the following two examples: 1. union test{ struct { int a; int b[]; }; }; int main(void){ union test test; test.a = 10; printf("test.b[0] = %d", test.b[0]); //prints 0, UB? } DEMO 2. #include union…
Some Name
  • 8,555
  • 5
  • 27
  • 77
15
votes
2 answers

Can we use pointer in union?

If no why? Uses of union over structure??
Chandrakant
  • 191
  • 1
  • 2
  • 4
15
votes
2 answers

Memory position of elements in C/C++ union

I have a union in C like this: union AUnion { struct CharBuf { char *buf; size_t len; } charbuf; uint8_t num; double fp_num; }; My question is, can I guarantee that if given the following: union AUnion u; Then the following are…
A_User
  • 397
  • 1
  • 9
15
votes
3 answers

Assignment between union members

Is this code well-defined? int main() { union { int i; float f; } u; u.f = 5.0; u.i = u.f; // ????? } It accesses two different union members in one expression so I am…
M.M
  • 138,810
  • 21
  • 208
  • 365
15
votes
1 answer

What is the use of the 'protected' keyword inside a union?

I checked that the protected access specifier can be used inside class, struct as well as union. I know that the protected access specifier means that members will be private, but visible to derived class. I am not able to think of a reasonable use…
anonymous
  • 1,920
  • 2
  • 20
  • 30
15
votes
2 answers

Why do unions have a deleted default constructor if just one of its members doesn't have one?

N3797::9.5/2 [class.union] says: If any non-static data member of a union has a non-trivial default constructor (12.1), copy constructor (12.8), move constructor (12.8), copy assignment operator (12.8), move assignment operator (12.8), or …
user2953119
15
votes
2 answers

Can you assign the value of one union member to another?

Consider the following code snippet: union { int a; float b; }; a = /* ... */; b = a; // is this UB? b = b + something; Is the assignment of one union member to another valid?
The Mask
  • 17,007
  • 37
  • 111
  • 185
15
votes
2 answers

Boost Variant essentially a Union in c/c++?

I'm wondering what the differences are between a Boost Variant and a union data-type in c/c++. I know that a union data-type takes up the same memory location and the largest data type in the region of memory occupies the total amount of memory used…
pandoragami
  • 5,387
  • 15
  • 68
  • 116
14
votes
1 answer

Union of layout-compatible types

Look at this code: struct A { short s; int i; }; struct B { short s; int i; }; union U { A a; B b; }; int fn() { U u; u.a.i = 1; return u.b.i; } Is it guaranteed that fn() returns 1? Note: this is a follow-up…
geza
  • 28,403
  • 6
  • 61
  • 135
14
votes
3 answers

Using unions to simplify casts

I realize that what I am trying to do isn't safe. But I am just doing some testing and image processing so my focus here is on speed. Right now this code gives me the corresponding bytes for a 32-bit pixel value type. struct Pixel { unsigned…
Steven Lu
  • 41,389
  • 58
  • 210
  • 364
14
votes
4 answers

Empty struct and anonymous union weird case

Compiling my code as C++11 with gcc 4.8.2 and llvm/clang 3.4 on fedora-linux, I got strange results that I couldn't really explain... here is a similar program fedora. #include using namespace std; struct A {}; struct C {}; struct B1 : A…
user3608817
  • 141
  • 3
14
votes
3 answers

Common initial sequence and alignment

While thinking of a counter-example for this question, I came up with: struct A { alignas(2) char byte; }; But if that's legal and standard-layout, is it layout-compatible to this struct B? struct B { char byte; }; Furthermore, if we…
dyp
  • 38,334
  • 13
  • 112
  • 177
14
votes
4 answers

Working with a union of structs in C

Say I have the following types: typedef struct TYPEA { int type; char[12] id; } TYPEA; typedef struct TYPEB { int type; int value; } TYPEB; I want to use create a union of these types and 'int', so that I can access the "type" int…
rikkit
  • 1,127
  • 3
  • 18
  • 35
14
votes
4 answers

Structures inside union

int main() { union { struct { char c[2]; char ch[2]; } s; struct { int i; int j; } st; } u = { (12, 1), (15, 1) }; printf("%d %d ", u.st.i, u.st.j); } How come the above is printing "257 0"? What…
user1915016
  • 176
  • 1
  • 12