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
9
votes
2 answers

Is it legal to cast a struct to an union containing it?

This is a followup to a question about printing a common member from different structs I thought that unions permit to examine the common initial sequence of two of their elements. So I ended with the following code: #include struct foo…
Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
9
votes
3 answers

Union of same type in C++

Whenever I see examples of union, they are always different types. For example, from MSDN: // declaring_a_union.cpp union DATATYPE // Declare union type { char ch; int i; long l; float f; double d; } var1; //…
Samaursa
  • 16,527
  • 21
  • 89
  • 160
9
votes
2 answers

Is it a bad idea to create a generic "function pointer" union in C?

For a project I'm working on, it's desirable to have a generic "pointer to a function" type. However, in C, to have a pointer to a function, you need to specify the prototype in the type of the function pointer. For example, if I have the function…
fpf3
  • 421
  • 6
  • 11
9
votes
1 answer

The purpose of union with only one member inside the struct

I saw such union inside struct definition in pure c code in Linux kernel sources struct cma_multicast (it's not the only one place. Seems that it is some common practice): struct cma_multicast { struct rdma_id_private *id_priv; union { …
budoattack
  • 379
  • 3
  • 11
9
votes
6 answers

Access struct members as if they are a single array?

I have two structures, with values that should compute a pondered average, like this simplified version: typedef struct { int v_move, v_read, v_suck, v_flush, v_nop, v_call; } values; typedef struct { int qtt_move, qtt_read, qtt_suck,…
DrBeco
  • 11,237
  • 9
  • 59
  • 76
9
votes
1 answer

How to write operator= for anonymous union with non-trivial members with virtual methods

C++11 gaves us the ability to create anonymous unions with non-trivial members. That can be very useful sometimes - for example, if I want to create Holder class for some non-trivial object without default ctor. Let's make this NonTrivial object…
Amomum
  • 6,217
  • 8
  • 34
  • 62
9
votes
2 answers

Use of Union with reference

At work I've been using linux and the GCC compiler for C++11 and C++14. In some of the code at work, I've used a union to store both a reference and a pointer, as so: (Simplified to just the important parts) struct MyStruct { //Stuff union {…
Alecto Irene Perez
  • 10,321
  • 23
  • 46
9
votes
1 answer

F# generic function filter list of discriminated unions

F# newbie question. I have a list of discriminated unions like: type Type1 = { name:string; id: int; } type Type2 = { x: float; y: float;} type Type3 = { x: float; naam:string} type Union1 = | T1 of Type1 | T2 of Type2 | T3 of Type3…
Rob Frohwein
  • 479
  • 2
  • 10
9
votes
4 answers

c union and bitfields

Can bitfields be used in union?
divya
  • 99
  • 1
  • 1
  • 2
9
votes
2 answers

C++14 constexpr union conditional initialization in constructor

I would like to pick the union member initialized in the constructor based on an argument. The following is an example that works: struct A { union { int i; float f; }; A(double d, bool isint) { if (isint)…
cshelton
  • 360
  • 3
  • 8
9
votes
4 answers

What's the use of const union members? Aren't they quite pointless?

In the comments to this answer, Koushik raised a very valid point. Take the following: union U { int x; const T y; }; (I choose T such that there is no common initial sequence of layout compatibility here, meaning only one member may be…
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
9
votes
5 answers

memcpy a void pointer to a union

Code: union foo { char c; int i; }; void func(void * src) { union foo dest; memcpy(&dest, src, sizeof(union foo)); //here } If I call func() like this: int main() { char c; int i; func(&c); func(&i); return…
Yu Hao
  • 119,891
  • 44
  • 235
  • 294
9
votes
2 answers

C++11 "In class initialization" feature is not working for unions

Minimal code example: struct B { union U { struct S {} s; int i = 100; } u; }; Now if we declare a B obj; then the obj.u.i is assigned a garbage value instead of 100. See the demo here. (The garbage value differs based on…
iammilind
  • 68,093
  • 33
  • 169
  • 336
9
votes
1 answer

Unions in C#: Structure Members Do Not Seem to be Aligned

I have defined to following structures to emulate a C++ union (which will eventually be used for C++ Interop): [StructLayout(LayoutKind.Sequential)] internal struct STRUCT1 { public Guid guid; public String str1; public String…
LJ VanKuiken
  • 141
  • 6
9
votes
2 answers

How Do I Properly Declare a ctype Structure + Union in Python?

I'm messing around with making a binary data parser, and while I could fall back on C, I wanted to see if I could use Python for the task. I have some inkling of how to get this going, and my current implementation looks something like this: from…
Casey K.
  • 188
  • 1
  • 6