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

unions and ranged variables in C

I have a couple of question. I need to write a struct that will include a field that will be either empty or of some specific type, using union. which one it is should be decided by the value of a bool variable (inRoom in this case). so this is…
littlerunaway
  • 443
  • 2
  • 8
  • 18
0
votes
2 answers

Representing Registers using Unions

I am trying to set up the internal registers of a HCS12 processor using unions. Here is the way I currently have the unions: union byte{ struct{ unsigned int B0: 1; unsigned int B1: 1; unsigned int B2: 1; unsigned…
Nathan
  • 1
  • 1
0
votes
0 answers

Why does this struct's explicit layout not act how I thought?

First off, sorry about the vague title, but I couldn't think of a better one. Basically, I have a struct defined as so: [StructLayout(LayoutKind.Explicit)] public struct SinkHead { [FieldOffset(0)] public long AsLong; [FieldOffset(1)] …
Xenoprimate
  • 7,691
  • 15
  • 58
  • 95
0
votes
1 answer

Unions script based on datasets circles

Can someone tell me if I made this good? I am not so sure, especially about one thing explained by second diagram: does this green region means values of X AND Z, or rather X OR Z? I made some corrects in code, but it seems that I am not using…
Bartłomiej Sobieszek
  • 2,692
  • 2
  • 25
  • 40
0
votes
1 answer

How to handle customized floating point using union(bit fields)?

I am analyzing a predecessor codes(codes run on microcontroller) handling pointing point, but I don't understand how things work. I have got to know how to convert flat to decimal and the other way around. However, what he did was used customized…
Jin
  • 113
  • 11
0
votes
2 answers

Pointer of type union (which also contains a struct) to an array of floats C

I have an array of floating point values which represents an a series of complex numbers (so the first value of the array, say x[0], is the real part of the first complex number, x[1] is the imaginary part of the first complex number, x[2] is the…
bluefocs
  • 63
  • 1
  • 9
0
votes
3 answers

Compilation error about a union field in another union

Consider below code, I have written: #include #include union myAccess { uint16_t access16; struct { uint8_t lo; uint8_t hi; } access8; }; union myByte{ uint8_t BYTE; struct { …
Gaurav K
  • 2,864
  • 9
  • 39
  • 68
0
votes
2 answers

Endianness within uint12 struct

Let's say I have data, such as below: union { struct { char flags : 4; uint16_t : 12; } char data[2]; } I understand how to make this code run regardless of byte endianness on a platform. I am asking to be sure that my…
Andrew Williamson
  • 8,299
  • 3
  • 34
  • 62
0
votes
2 answers

How to declare anonymously

I have the following problem: I'm trying to define some basic struct, that helps me map a part of the controller memory to use it in more efficient way. Let me present you an example: typedef struct { ICR1_t ICR1_dByte; /* 0x46 - 0x47 …
Tomas
  • 3,269
  • 3
  • 29
  • 48
0
votes
1 answer

Unable to extract byte array value from int value

A union is defined, and given an integer value. The required size of array is estimated. Following the value is defined to the union. However, the byte array values are not able to be printed (ie. the last portion of the following code is not…
0
votes
3 answers

union initializing in c

#include int main() { union emp; union emp{ int age; char name[2]; }; union emp e1={512}; printf("%d,%d,%d",e1.age,e1.name[0],e1.name[1]); return 0; } Here ive tried to craete a union and initialize its 1st…
Abhishek Dey
  • 175
  • 1
  • 6
0
votes
0 answers

scanner - parser error: yylval undefined

I'm trying to build a scanner-parser to handle input like this x1+x2+x3+x4; and I'm blocked by this error. I understand that when you declare a union in the flex flex2.y file the yylval is connected with this union and after you compile the file…
captain monk
  • 719
  • 4
  • 11
  • 34
0
votes
1 answer

How to perform an union of two files containing keywords in python?

I have 2 different text files. File1.txt contains: program RAM Python parser File2.txt contains: file 1234 program I want to perform an union of these both files such that i get an output file3.txt which…
Aaron Misquith
  • 621
  • 1
  • 7
  • 11
0
votes
2 answers

How do I use the constructors in a named union? And how can I change the values in the same union instance later? c++/c++11

I have a bit of code somewhat like this: class Token{ public: union tester{ double literal; string name; tester(double op) : literal(op) {}; tester(string val) : name(val) {}; tester(): tester(0.0)…
0
votes
3 answers

Union member data alignment

I'm having some problem with data alignment in a union, which I can't seem to figure out. Using a union similar to the one below, there seems to be some data offset at the union's base address. typedef union MyUnion { struct MyStruct { …
dsell002
  • 1,296
  • 2
  • 11
  • 24
1 2 3
99
100