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
8
votes
3 answers

Can we use va_arg with unions?

6.7.2.1 paragraph 14 of my draft of the C99 standard has this to say about unions and pointers (emphasis, as always, added): The size of a union is sufficient to contain the largest of its members. The value of at most one of the members can be…
Chris Lutz
  • 73,191
  • 16
  • 130
  • 183
8
votes
5 answers

Why is my union's size bigger than I expected?

When I print the size of a union like this: union u { char c[5]; int i; } un; using this: int _tmain(int argc, _TCHAR* argv[]) { printf("size of union = %d ",sizeof(un)); return 0; } I get an answer of 8 using Visual C++, but I…
Saurabh Ghorpade
  • 1,137
  • 5
  • 15
  • 22
8
votes
2 answers

Error in Accessing a Union Member in Constant Expression

I was doing some experiments with unions when I encountered a problem. union U { // struct flag for reverse-initialization of each byte struct rinit_t { }; constexpr static const rinit_t rinit{}; uint32_t dword; uint8_t byte[4]; …
AJ Tan
  • 193
  • 7
8
votes
3 answers

... with constructor not allowed in union problem

I desperately need to find a solution for the following problem: namespace test { template struct Flags { int _flags; Flags() { _flags = 0; } Flags(int flags) { …
Ryan
  • 1,451
  • 2
  • 27
  • 36
8
votes
3 answers

What's the correct way of using bitfields in C?

I am using bitfields to get easy access on a float library I am trying to make for a microcontroller with no FPU. The problem is that I can't seem to make it work with bitfields. Take a look: typedef struct { union{ unsigned long mantissa:…
Lefteris
8
votes
3 answers

C++ lifetime of union member

In the current version of the C++ standard draft, [basic.life]/1 states: The lifetime of an object or reference is a runtime property of the object or reference. A variable is said to have vacuous initialization if it is default-initialized and,…
user42768
  • 1,951
  • 11
  • 22
8
votes
1 answer

Can you write a copy constructor for a union with const members?

Suppose I have a struct that contains a union with const members, like so: struct S { // Members const enum { NUM, STR } type; union { const int a; const std::string s; }; // Constructors S(int t_a) : type(NUM), a(t_a); …
Daniel McLaury
  • 4,047
  • 1
  • 15
  • 37
8
votes
2 answers

union containing only one struct

I have started today to program on a PIC16f88, and found that the header for its registers contains a union that only contains a struct: extern volatile unsigned char ANSEL __at(0x09B); typedef union { struct { unsigned ANS0 :1; …
8
votes
1 answer

Do scalar members in a union count towards the common initial sequence?

In the union U below, if a or b is the active member, is it defined behavior to access c? struct A{ int a; }; struct B{ int a; double b; }; union U{ A a; B b; int c; }; In [class.union], the standard defines some rules to…
AndyG
  • 39,700
  • 8
  • 109
  • 143
8
votes
2 answers

Would this union work if char had stricter alignment requirements than int?

Recently I came across the following snippet, which is an attempt to ensure all bytes of i (nad no more) are accessible as individual elements of c: union { int i; char c[sizeof(int)]; }; Now this seems a good idea, but I wonder if the standard…
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
8
votes
2 answers

Writing a safe tagged union in C

Let's suppose you are writing a C struct which represents a course in a meal. One of the fields in the course struct is of type: enum TP_course {STARTER, MAINCOURSE, DESSERT}; Then, depending on the type of the course, you have a subtype: enum…
cesss
  • 852
  • 1
  • 6
  • 15
8
votes
1 answer

Is this use of unions in C valid/compliant?

Given these structures: typedef struct { //[...] } StructA; typedef struct { StructA a; //[...] } StructB; typedef union { StructA a; StructB b; } Union; Are the two access methods below equivalent and not undefined? Union…
Daniel Wyatt
  • 379
  • 3
  • 7
8
votes
4 answers

Conceptual problem in Union

My code is this // using_a_union.cpp #include union NumericType { int iValue; long lValue; double dValue; }; int main() { union NumericType Values = { 10 }; // iValue = 10 printf("%d\n",…
Joel
  • 83
  • 3
8
votes
0 answers

compiler is out of heap space in pass 2 - anonymous union, anonymous structs, constexpr constructor, static member

I have code that fails to compile with Visual Studio 2015 Community Edition with the following error: fatal error C1002: compiler is out of heap space in pass 2 The Code struct Int { int i; }; struct B { union { struct { int x; }; …
Daskie
  • 435
  • 3
  • 11
8
votes
1 answer

Changing active member of union in constant expressions

Playing with constexpr and union I found out, that I can't change active member of an union in constexpr. Just one exception: union of empty classes. constexpr bool t() { struct A {}; struct B {}; union U { A a; B b; } u{}; u.a =…
Tomilov Anatoliy
  • 15,657
  • 10
  • 64
  • 169