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

Mac OS X Yosemite Python union output error on Eclipse

No sure if appropriate to ask this questions here, since I am a complete noob in Python. I am following the instruction on Gray Hat Python and was working on union coding. this is the original code from ctypes import * class barley_amount(Union): …
Jim
  • 3
  • 1
0
votes
3 answers

Struct containing union containing struct pointer to self uses '.' access instead of '->', confused

In the codebase I'm working on, I have two structures defined like so: typedef struct s1 { int somedata; union su { char *cptr; struct s1 *array; }; //line 123 } S1; typedef struct s2 { S1 data; }…
Mike Dannyboy
  • 462
  • 1
  • 4
  • 13
0
votes
2 answers

How to portably allocate space for a particular member of a union embedded in a struct

Consider the following type in C11, where MyType1 and MyType2 are previously declared types: typedef struct { int tag; union { MyType1 type1; MyType2 type2; } } MyStruct; I'd like to allocate enough memory using malloc to hold the tag…
Marc
  • 4,327
  • 4
  • 30
  • 46
0
votes
0 answers

Initializing a struct within a union within a struct in C

struct huffnode{ int freq; union{ struct children { struct huffnode * l; struct huffnode * r; } char c; }un; }; I'm trying to create a program that implements a huffman tree but in order to do that I need to create…
0
votes
2 answers

Issues with union and memory aligment

i'm currently working on a Port of Embedded Code (on a Freescale S12) so GNU and i hava a issue with unions. i have the following union typedef signed short sint16; typedef signed long sint32; typedef union { sint32 Akku; …
0
votes
3 answers

union within struct. compilation warnings. c

I have the fallowing structs: struct lshort_sched_param { int requested_time; int level; }; struct sched_param { union { int sched_priority; struct lshort_sched_param lshort_params; }; }; and i'm trying to create a…
Bob Sacamano
  • 699
  • 15
  • 39
0
votes
1 answer

C++ struct in union acces over union member

i have a Struct which consists of an Union which consists of an uint32_t and another (anonym) struct. It looks like this: struct sMesswertNummer { union { uint32_t MesswertNummerFull; struct { uint16_t MesswertNummerPart:16; …
Haselnussstrauch
  • 333
  • 2
  • 10
0
votes
3 answers

Referencing a union inside a structure using union tag gives incorrect address

I had a need to declare a union inside a structure as defined below: struct MyStruct { int m_DataType; DWORD m_DataLen; union theData { char m_Buff [_MAX_PATH]; struct MyData m_myData; }…
AlanKley
  • 4,592
  • 8
  • 40
  • 53
0
votes
3 answers

Getter for private union - c++

I have a class Foo with a private union: class Foo { public: static Foo foo(int type); static Foo foo(double type); static Foo foo(bool type); static Bar getBar(Foo foo); private: union Bar { int iBar; …
MayNotBe
  • 2,110
  • 3
  • 32
  • 47
0
votes
1 answer

Redundant size of union compiling with -m32 on OS X

I have the following tagged union in my code: https://github.com/EarlGray/SECD/blob/f2e364f84d194aea5cef9257630bf931e9f88cab/secd.h#L217 When I compile it on 64 bit Linux or OS X using gcc or clang, size of cell_t is always 32 bytes (4 *…
Dmytro Sirenko
  • 5,003
  • 21
  • 26
0
votes
1 answer

What is union doing to help translate a byte array into a different type such as a word in this c++ code?

I am looking at some c++ code and I want to find out what union is doing to help translate a byte array into, well a different type such as a word. At least that is what I think is going on. Truly what I want is to figure out the purpose for this…
amalgamate
  • 2,200
  • 5
  • 22
  • 44
0
votes
1 answer

C Union and simultaneous assignment to members

In the following code #include int main() { union myUnion { int intVar; char charVar; float floatVar; }; union myUnion localVar; localVar.intVar = 10; localVar.charVar = 'A'; …
Sagrian
  • 1,048
  • 2
  • 11
  • 29
0
votes
0 answers

How to initialize an unnamed structure element within a named union

I am using a union and an anonymous structure inside it. I want to know how I can initialize the structure members inside it. how can i do that? Please help :) typedef union { uint8_t All; struct { uint8_t G:1; …
Ananth
  • 21
  • 8
0
votes
2 answers

accessing unassigned value of a different type in a union

I have this code: typedef union MyUnion { int ival; float fval; } MyUnion; MyUnion myUnion; myUnion.fval = 3.0f; someFuncCall(myUnion.ival); What exactly am I doing when I ask for the ival? It is my guess that I am asking for the float to…
eatonphil
  • 13,115
  • 27
  • 76
  • 133
0
votes
1 answer

Creating a typemap in SWIG for a struct containing a union (Python)

I am new to using SWIG and struggling with making a Python typemap for the following struct: typedef struct si2drExprT { si2drExprTypeT type; union { si2drInt32T i; si2drFloat64T d; si2drStringT s; /* most…
HGarrett
  • 21
  • 3