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
53
votes
7 answers

Anonymous union within struct not in c99?

here is very simplified code of problem I have: enum node_type { t_int, t_double }; struct int_node { int value; }; struct double_node { double value; }; struct node { enum node_type type; union { struct int_node…
Martin
  • 1,473
  • 2
  • 12
  • 20
52
votes
8 answers

sizeof a union in C/C++

What is the sizeof the union in C/C++? Is it the sizeof the largest datatype inside it? If so, how does the compiler calculate how to move the stack pointer if one of the smaller datatype of the union is active?
Naveen
  • 74,600
  • 47
  • 176
  • 233
48
votes
2 answers

Interface for associative object array in TypeScript

I have an object like so: var obj = { key1: "apple", key2: true, key3: 123, . . . key{n}: ... } So obj can contain any number of named keys, but the values must all be either string, bool, or number. How do I declare the…
prmph
  • 7,616
  • 11
  • 37
  • 46
47
votes
4 answers

Appending/concatenating two IEnumerable sequences

I have two sets of datarows. They are each IEnumerable. I want to append/concatenate these two lists into one list. I'm sure this is doable. I don't want to do a for loop and noticed that there is a Union method and a Join method on the two Lists.…
MikeTWebb
  • 9,149
  • 25
  • 93
  • 132
47
votes
7 answers

How to check what type is currently used in union?

let's say we have a union: typedef union someunion { int a; double b; } myunion; Is it possible to check what type is in union after I set e.g. a=123? My approach is to add this union to some structure and set uniontype to 1 when it's int…
tomdavies
  • 1,896
  • 5
  • 22
  • 32
40
votes
7 answers

Unions as Base Class

The standard defines that Unions cannot be used as Base class, but is there any specific reasoning for this? As far as I understand Unions can have constructors, destructors, also member variables, and methods to operate on those varibales. In short…
Alok Save
  • 202,538
  • 53
  • 430
  • 533
39
votes
6 answers

A question about union in C - store as one type and read as another - is it implementation defined?

I was reading about union in C from K&R, as far as I understood, a single variable in union can hold any one of the several types and if something is stored as one type and extracted as another the result is purely implementation defined. Now please…
whacko__Cracko
  • 6,592
  • 8
  • 33
  • 35
38
votes
8 answers

Examples of Union in C

I'm looking for some union examples, not to understand how union works, hopefully I do, but to see which kind of hack people do with union. So feel free to share your union hack (with some explanation of course :) )
claf
  • 9,043
  • 17
  • 62
  • 79
38
votes
3 answers

Why is it invalid for a union type declared in one function to be used in another function?

When I read ISO/IEC 9899:1999 (see:6.5.2.3), I saw an example like this (emphasis mine) : The following is not a valid fragment (because the union type is not visible within function f): struct t1 { int m; }; struct t2 { int m; }; int f(struct t1 *…
kangjianwei
  • 900
  • 9
  • 14
38
votes
7 answers

gcc, strict-aliasing, and casting through a union

Do you have any horror stories to tell? The GCC Manual recently added a warning regarding -fstrict-aliasing and casting a pointer through a union: [...] Taking the address, casting the resulting pointer and dereferencing the result has undefined…
Joseph Quinsey
  • 9,553
  • 10
  • 54
  • 77
37
votes
3 answers

Is a Union Member's Destructor Called

C++11 allowed the use of standard layout types in a union: Member of Union has User-Defined Constructor My question then is: Am I guaranteed the custom destructor will be called, when the union goes out of scope? My understanding is that we must…
Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288
37
votes
3 answers

Union initialization in C++ and C

I have built a working C library, that uses constants, in header files defined as typedef struct Y { union { struct bit_field bits; uint8_t raw[4]; } X; } CardInfo; static const CardInfo Y_CONSTANT = { .raw = {0, 0, 0, 0 } }; I know…
Alexander Oh
  • 24,223
  • 14
  • 73
  • 76
36
votes
3 answers

What is a designated initializer in C?

I have an assignment that requires me to understand what are designated initializers in C, and what it means to initialize a variable with one. I am not familiar with the term and couldn't find any conclusive definitions. What is a designated…
user7349461
  • 387
  • 1
  • 3
  • 5
36
votes
1 answer

Is there any difference between structure and union if we have only one member?

I would like to know the difference between structure and union for one member data type if there is any.
RAFI KC
  • 421
  • 4
  • 8
32
votes
3 answers

Can a union be initialized in the declaration?

For example, say we have a union typedef union { unsigned long U32; float f; }U_U32_F; When a variable of this union type is declared, is there a way to set an initial value? U_U32_F u = 0xffffffff; // Does not work...is there a correct syntax…
semaj
  • 1,555
  • 1
  • 12
  • 25