unions Questions

5

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 a...
Inhabitancy asked 7/5, 2014 at 18:46

1

Solved

This code is an attempt to reinterpret memory contents as a different type without violating strict aliasing rules. It was suggested as an answer to "Using std::memmove to work around strict a...
Discharge asked 20/9 at 13:37

3

Solved

At work I've been using linux and the GCC compiler for C++11 and C++14. In some of the code at work, I've used a union to store both a reference and a pointer, as so: (Simplified to just the import...
Contrayerva asked 1/8, 2016 at 4:58

7

Solved

How can I declare and use a C union type in Swift? I tried: var value: union { var output: CLongLong var input: [CInt] } but it does not work... UPDATED: I want to use union to split a 8 by...
Battled asked 21/6, 2015 at 7:55

2

Solved

I am trying to define a union struct with some struct and primitive members overlapping in memory with a simple array. This works perfectly in Clang and MSVC, but it doesn't compile with GCC (G++)....
Namnama asked 6/5, 2022 at 6:50

7

Solved

I have two structures, with values that should compute a pondered average, like this simplified version: typedef struct { int v_move, v_read, v_suck, v_flush, v_nop, v_call; } values; typedef st...
Unemployment asked 2/4, 2011 at 17:0

8

I have an array of 100 uint8_t's, which is to be treated as a stream of 800 bits, and dealt with 7 bits at a time. So in other words, if the first element of the 8-bit array holds 0b11001100 and th...
Tutu asked 28/6, 2017 at 18:49

4

Solved

I have built a working C library, that uses constants, in header files defined as typedef struct Y { union { struct bit_field bits; uint8_t raw[4]; } X; } CardInfo; static const CardInfo Y_CO...
Tab asked 19/7, 2012 at 7:16

5

I want to narrow a string to a string literal union. In other words, I want to check if the string is one of the possible values of my literal union, so that this will work (if the operator couldbe...
Buckbuckaroo asked 16/5, 2017 at 14:43

21

When should unions be used? Why do we need them?
Gimmick asked 31/10, 2008 at 3:51

9

Solved

Is there an easy explanation for what this error means? request for member '*******' in something not a structure or union I've encountered it several times in the time that I've been learning C...
Titillate asked 2/2, 2010 at 13:46

7

Solved

let's say we have a union: typedef union someunion { int a; double b; } myunion; Is it possible to check what type is in union after I set e.g. a=123? My approach is to add this union to some ...
Emsmus asked 18/5, 2013 at 10:28

2

This seems to be similar to POD structs containing constant member, but sort of reversed. #include <iostream> struct A { int a; }; union U { volatile A a; long b; }; int main() { U u1...
Enneahedron asked 5/2, 2015 at 1:53

7

Solved

Since python is dynamically typed, of course we can do something like this: def f(x): return 2 if x else "s" But is this the way python was actually intended to be used? Or in other words, do u...
Gourde asked 9/8, 2016 at 15:1

1

Solved

In C++, unions can only have zero or one active members at any given time, and the C++ standard provides few ways to make a member active. One such way is by direct assignment in a statement like u...
Yawning asked 27/4, 2023 at 20:12

3

Solved

I have an assignment that requires me to understand what are designated initializers in C, and what it means to initialize a variable with one. I am not familiar with the term and couldn't find any...
Conch asked 9/11, 2017 at 13:3

2

Solved

I wanted to shorten my code, the code's purpose is to control stepper motors. I want to hold the ammount of steps (32 bits) and the frequency of rotation (16 bits). I receive this information throu...
Livorno asked 11/1, 2023 at 22:3

6

Solved

If I have a class: class Odp { int i; int b; union { long f; struct { WCHAR* pwszFoo; HRESULT hr; }; }; } Union means that, of all values listed, it can only take on one of those val...
Manful asked 18/6, 2010 at 17:23

1

Solved

Suppose sizeof( int ) == sizeof( float ), and I have the following code snippet: union U{ int i; float f; }; U u1, u2; u1.i = 1; //i is the active member of u1 u2.f = 1.0f; //f is the active mem...
Majestic asked 31/8, 2022 at 11:39

2

Solved

Sort of related to my previous question: Do elements of arrays count as a common initial sequence? struct arr4 { int arr[4]; }; struct arr2 { int arr[2]; }; union U { arr4 _arr4; arr2 _arr2; }...
Rolanda asked 17/3, 2016 at 3:20

2

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{}...
Lindsley asked 5/9, 2020 at 11:11

2

Solved

I have an object like so: var obj = { key1: "apple", key2: true, key3: 123, . . . key{n}: ... } So obj can contain any number of named keys, but the values must all be either string, bool...
Mcallister asked 5/7, 2016 at 22:35

4

Solved

Is the following undefined behaviour? union { int foo; float bar; } baz; baz.foo = 3.14 * baz.bar; I remember that writing and reading from the same underlying memory between two sequence ...
Blois asked 22/10, 2015 at 21:31

2

Solved

This is a followup to a question about printing a common member from different structs I thought that unions permit to examine the common initial sequence of two of their elements. So I ended with ...
Cinemascope asked 11/3, 2021 at 10:16

7

Solved

I'm translating a library written in C++ to C#, and the keyword 'union' exists once. In a struct. What's the correct way of translating it into C#? And what does it do? It looks something like thi...
Verdict asked 24/9, 2008 at 12:20

© 2022 - 2024 — McMap. All rights reserved.