unions

Is there a way to access individual bits with a union?

牧云@^-^@ 提交于 2019-12-03 05:54:56
问题 I am writing a C program. I want a variable that I can access as a char but I can also access the specific bits of. I was thinking I could use a union like this... typedef union { unsigned char status; bit bits[8]; }DeviceStatus; but the compiler doesn't like this. Apparently you can't use bits in a structure. So what can I do instead? 回答1: Sure, but you actually want to use a struct to define the bits like this typedef union { struct { unsigned char bit1 : 1; unsigned char bit2 : 1; unsigned

Is it legal and well defined behavior to use a union for conversion between two structs with a common initial sequence (see example)?

妖精的绣舞 提交于 2019-12-03 05:13:31
I have an API with a publicly facing struct A and an internal struct B and need to be able to convert a struct B into a struct A. Is the following code legal and well defined behavior in C99 (and VS 2010/C89) and C++03/C++11? If it is, please explain what makes it well-defined. If it's not, what is the most efficient and cross-platform means for converting between the two structs? struct A { uint32_t x; uint32_t y; uint32_t z; }; struct B { uint32_t x; uint32_t y; uint32_t z; uint64_t c; }; union U { struct A a; struct B b; }; int main(int argc, char* argv[]) { U u; u.b.x = 1; u.b.y = 2; u.b.z

Union in c++ are they feasible

邮差的信 提交于 2019-12-03 04:39:22
Can a union in C++ have a member function? How do union with data members and member functions exist if an object is created? If I suppose yes, then are they feasible any where. If yes then where? 9.5/1 A union can have member functions (including constructors and destructors), but not virtual (10.3) functions. A union shall not have base classes. A union shall not be used as a base class. An object of a class with a non-trivial constructor (12.1), a non-trivial copy constructor (12.8), a non-trivial destructor (12.4), or a non-trivial copy assignment operator (13.5.3, 12.8) cannot be a member

select an union member depending on a template parameter

会有一股神秘感。 提交于 2019-12-03 00:27:34
I'm dealing with an union in C++, and I would like have a function template which access to the active union member depending on a template parameter. Code is something like (doSomething is just an example): union Union { int16_t i16; int32_t i32; }; enum class ActiveMember { I16 , I32 } template <ActiveMember M> void doSomething(Union a, const Union b) { selectMemeber(a, M) = selectMember(b, M); // this would be exactly (not equivalent) the same // that a.X = b.X depending on T. } To accomplish this I only found bad hacks like specialization, or a not homogeneous way to access and assign. I'm

How to know that which variable from Union is Used?

亡梦爱人 提交于 2019-12-02 21:59:51
If I declare a Union as: union TestUnion { struct { unsigned int Num; unsigned char Name[5]; }TestStruct; unsigned char Total[7]; }; Now, How can I know that whether Total[7] is used or TestStruct is used? I am using C! I was revisiting unions and structures and this question came to my mind. " sizeof " can't be used as both are of same size i.e. 7 bytes. (And Here comes another question) When I filled only "Total" with a Character 'a' and Tried sizeof(TestUnionInstance) , it returned 12 (Size of Char is 1 byte, Right?). So I isolated the structure from it and found that Size of Structure is

Unions as Base Class

折月煮酒 提交于 2019-12-02 20:09:28
The standard defines that Unions cannot be used as Base class, but is there any specific reasoning for this? As far as I understand Unions can have constructors, destructors, also member variables, and methods to operate on those varibales. In short a Union can encapsulate a datatype and state which might be accessed through member functions. Thus it in most common terms qualifies for being a class and if it can act as a class then why is it restricted from acting as a base class? Edit: Though the answers try to explain the reasoning I still do not understand how Union as a Derived class is

Unions versus structures in C

送分小仙女□ 提交于 2019-12-02 19:40:05
The idea behind this question is to understand the deeper concepts of using union and using it in different way so as to save memory.. My question to all is-- let's say there is a structure struct strt { float f; char c; int a; } and the same structure represented in union union unin { float f; char c; int a; } If I allocate values to structure members one after another and then print them, it gets printed. But in case of union it doesn't happen, some overwriting is being done.. So I need to find out a method which can store the values of f,c,a using union and then I can print the same. (Apply

Is there a way to access individual bits with a union?

孤者浪人 提交于 2019-12-02 19:18:30
I am writing a C program. I want a variable that I can access as a char but I can also access the specific bits of. I was thinking I could use a union like this... typedef union { unsigned char status; bit bits[8]; }DeviceStatus; but the compiler doesn't like this. Apparently you can't use bits in a structure. So what can I do instead? Sure, but you actually want to use a struct to define the bits like this typedef union { struct { unsigned char bit1 : 1; unsigned char bit2 : 1; unsigned char bit3 : 1; unsigned char bit4 : 1; unsigned char bit5 : 1; unsigned char bit6 : 1; unsigned char bit7 :

Union in Struct Error

戏子无情 提交于 2019-12-02 10:28:54
I have the following struct: struct type1 { struct type2 *node; union element { struct type3 *e; int val; }; }; When initialising a pointer *f that points to an instance of type1 and doing something like: f.element->e or even just f.element , I get: error: request for member ‘element’ in something not a structure or union What am I overseeing here? element is the name of the union, not the name of a member of type1 . You must give union element a name: struct type1 { struct type2 *node; union element { struct type3 *e; int val; } x; }; then you can access it as: struct type1 *f; f->x.e If f is

Does this union break strict aliasing? What about floating point registers

我是研究僧i 提交于 2019-12-02 02:26:14
问题 union { Uint32 Integer; Float32 Real; } Field; I have to use that union for a little IEEE trick, does that break strict aliasing? GCC is not throwing any warning(tried with GCC 4.5 and 4.6 even with pedantic strict aliasing, but As Far As I Know, GCC is not very good catching strict aliasing rules infringiments (lots of false positives/negatives). Field A; A.Integer = (Value1 & B) || Value2; return A.Real; That's the snippet I'm currently using, it seems working correctly without any warning,