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
6
votes
1 answer

What's the struct's initial sequence?

I came across the initial sequence concept. Serching through the Standard for initial sequence phrase gives only 3 results and they don't give a definition. Section N3797::9.5/1 [class.union]: If a standard-layout union contains several…
user2953119
6
votes
4 answers

Implementing a c/c++ style union as a column in MySQL

Friends, I have a strange need and cannot think my way through the problem. The great and mighty Google is of little help due to keyword recycling (as you'll see). Can you help? What I want to do is store data of multiple types in a single column…
Michael
  • 93
  • 4
6
votes
2 answers

pointer to union member

I have a struct: struct TypeValue{ u8 type; union{ u8 u8value; s8 s8value; }value; } Depending on type var we can have value from u8value or s8value. Now I have a struct TypeValue and I want to get the void pointer to the value element…
Maluvel
  • 425
  • 1
  • 6
  • 13
6
votes
3 answers

C++: String and unions bison

I am building a compiler in flex and bison. The thing is that using char * is giving a lot of problems so I'm trying to migrate everything to string. The only problem left is that there is a union with strings. I know that this is not a standard,…
rforseck
  • 63
  • 1
  • 4
6
votes
3 answers

Get sizeof anonymous struct inside union

I'm hoping this isn't a duplicate question, but I've searched in some detail and haven't found my exact case before. I have a simple struct that I also want to be able to access as a simple byte array union { struct { unsigned char a; …
gkimsey
  • 517
  • 6
  • 13
6
votes
4 answers

Invalid union member

Is there a way in Visual Studio to handle non-trivial unions. The following code is running fine using g++ -std=c++11 but VS complains: invalid union member -- class "Foo" has a disallowed member function The code is as follows: struct Foo { …
bagage
  • 1,094
  • 1
  • 21
  • 44
6
votes
1 answer

Marshal Union With Arrays

I've run into an odd scenario marshaling unions that contain arrays in C#/.NET. Consider the following program: namespace Marshal { class Program { [StructLayout(LayoutKind.Sequential, Pack = 1)] struct InnerType { …
watkipet
  • 959
  • 12
  • 23
6
votes
2 answers

How is a struct stored in memory?

I have a struct iof_header in my code, and I determined it would be 24 bytes wide. I perform a sizeof(iof_header) and it returns 32 bytes wide. Question 1 Why is it 32 bytes wide instead of 24? Question 2 Including its members, how is a struct…
Josh C
  • 1,035
  • 2
  • 14
  • 27
6
votes
2 answers

Why it is printing reverse string?

My expected output for u6.c was ABC but here I got CBA why is it so? Could you please shed some light on this with detailed explanation? union mediatech { int i; char c[5]; }; int main(){ mediatech u1 = {2}; // 1 mediatech u2 =…
uss
  • 1,271
  • 1
  • 13
  • 29
6
votes
2 answers

Unrestricted union in practice

I have some questions about unrestricted unions and their application in practice. Let's suppose I have the following code : struct MyStruct { MyStruct(const std::vector& a) : array(a), type(ARRAY) {} MyStruct(bool b) : boolean(b),…
3XX0
  • 1,315
  • 1
  • 13
  • 25
6
votes
2 answers

union can't contain an object that has a virtual function

Code: struct A{ int a; virtual void f(){} }; union B{ A ob; }; Compile-time Error: C:\to\main.cpp|9|error: member 'A B::ob' with constructor not allowed in union| C:\to\main.cpp|9|error: member 'A B::ob' with copy assignment operator…
AlexDan
  • 3,203
  • 7
  • 30
  • 46
6
votes
2 answers

Implementing function delegates in C with unions and function pointers

I'd like to be able to generically pass a function to a function in C. I've used C for a few years, and I'm aware of the barriers to implementing proper closures and higher-order functions. It's almost insurmountable. I scoured StackOverflow to see…
Philip Conrad
  • 1,451
  • 1
  • 13
  • 22
6
votes
1 answer

Objective-C KVO doesn't work with C unions

I need to observe union-typed properties on an Objective-C class using KVO, but it seems I have no luck with this. I did some experiments: everything works fine as long as I am using a C struct. As soon as I replace the struct with a union,…
starbugs
  • 992
  • 1
  • 9
  • 14
6
votes
2 answers

type namespace in C

I've read in SO about different namespaces in C where the type are defined, e.g. there is a namespace for Structs and Unions and a namespace for typedefs. Is namespace the exact name for this? How many namespaces exist in C?
Clynamen
  • 509
  • 1
  • 6
  • 16
5
votes
3 answers

How to determine what type is used in a union?

Is it possible to determine what type a union contain if there are several possible choices? typedef union { char charArr[SIZE]; int intVal; float floatVal; } VALUE_TYPE; VALUE_TYPE number; number.intVal = 8; How to know…
norq
  • 1,404
  • 2
  • 18
  • 35