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

C++ Union usage

So far I have just used unions to store either member A or member B. I do now find myself in the case where I want to change the used member during runtime. union NextGen { std::shared_ptr Child = nullptr; …
Noel Widmer
  • 4,444
  • 9
  • 45
  • 69
7
votes
3 answers

Using inheritance within a union

I was was wondering if anyone knows if it's possible to use inheritance within a union somehow. In the example below, the TestFails union will not contain the a variable within the Base struct, while TestWorks does work. struct Base { int a;…
Artoo
  • 116
  • 7
7
votes
2 answers

Anonymous Union in Struct Initializer

Why does following example not work in C? #include typedef struct { int x; } X; typedef struct { char y[10]; } Y; typedef struct { int pos; union { X x; Y y; }; } Test; int main() { X x = { 65 }; Y y = { "Hallo"…
WaeCo
  • 1,155
  • 1
  • 10
  • 21
7
votes
3 answers

C++11 move constructor for union-like class

Is there a better way to build a move constructor for a union-like class? If I were to have a union-like class like the class in the following code, is there a way to build the class or the move constructor that wouldn't require switch statement…
Daniel Robertson
  • 1,354
  • 13
  • 22
7
votes
2 answers

error: aggregate value used where an integer was expected

I am having following union union data { uint64_t val; struct{ .... } }; and I have a function func(union data mydata[]) { printf("%llu",(uint64_t)mydata[0]); // Here is the error } When i compile this code it is giving…
Chinna
  • 3,930
  • 4
  • 25
  • 55
7
votes
2 answers

C Inheritance with Unions

I was reading somewhere about how some object oriented features can be implemented in C, and it has proven fairly useful. In specific, I have been toying with the idea of inheritance. Here is an example: typedef struct Circle{ int rad, x, y; …
BrainSteel
  • 669
  • 5
  • 17
7
votes
3 answers

Union of structs with common first member

I am unsure of whether or not the code has pointer aliasing (or other standard conformance issues) in the asserts cast. It seems that a pointer to the union type should be able to be cast to a pointer of the first member and since the union is only…
backscattered
  • 185
  • 1
  • 11
7
votes
1 answer

Constructing a union tuple in C++11

Tuples are kind of like structs. Are there also tuples that behave like unions? Or unions where I can access the members like in tuples, e.g. my_union_tuple u; get<1>(u); get(u); // C++14 only, or see below For the 2nd line, see…
Johannes
  • 2,901
  • 5
  • 30
  • 50
7
votes
4 answers

Casting to union

Having limited experience with unions in C++, I am struggling to understand the basic mechanics of casting to/from that datatype. Suppose two types Type_a and Type_b are wrappers of type int and long respectively. A union is defined as: union Type_u…
BlackVegetable
  • 12,594
  • 8
  • 50
  • 82
7
votes
3 answers

C Union in C# Error incorrectly aligned or overlapped by a non-object field

I'm working on a C# WPF application and trying to use an unmanaged dll (don't have access to source). The structure I need is 'NET_DVR_IPPARACFG_V40' which contains a bunch of other structs/unions. Here is the info the documentation gives…
Brian
  • 819
  • 4
  • 20
  • 35
7
votes
1 answer

Does C++11 allow non-anonymous unions to contain static data members?

In C++11 I declare the following union: union U4 { char c; int i; static int si; }; When I compile this code with g++ 4.7.0 using -std=c++11 -pedantic-errors, I get the following errors (with minor editing): error: local class ‘union…
sschurr
  • 293
  • 1
  • 8
7
votes
5 answers

What is the point behind unions in C?

I'm going through O'Reilly's Practical C Programming book, and having read the K&R book on the C programming language, and I am really having trouble grasping the concept behind unions. They take the size of the largest data type that makes them…
user978122
  • 5,531
  • 7
  • 33
  • 41
7
votes
3 answers

select an union member depending on a template parameter

I'm dealing with an union in C++, and I would like have a function template which access to the active union member depending on a template parameter. Code is something like (doSomething is just an example): union Union { int16_t i16; int32_t…
user1476999
  • 177
  • 1
  • 8
7
votes
1 answer

C: typedef union

didn't find anything in related questions. Most probably it's super noob, but I'll ask anyway/ I've got the following in my .h file: typedef union _API_Packet_0x90{ uint8_t packet[26]; struct _pack_struct { uint8_t start; …
dccharacter
  • 421
  • 1
  • 4
  • 14
7
votes
1 answer

Which standards allow anonymous structs and unions in C and C++?

Where can we use anonymous structs and unions? struct { int bar; }; // anonymous struct union { int bar; }; // anonymous union I think that we can do it in the following standards: unions - C++98, C++03, C++11, C11 structs - C11 Am i…
FrozenHeart
  • 19,844
  • 33
  • 126
  • 242