Can a union be initialized in the declaration?
Use an initializer list: U_U32_F u = { 0xffffffff }; You can set other members than the first one via U_U32_F u = { .f = 42.0 };
Use an initializer list: U_U32_F u = { 0xffffffff }; You can set other members than the first one via U_U32_F u = { .f = 42.0 };
You can still initialize the union as follows: const union un a = { .i = 100 }; then use it in your code.
I usually use unions when parsing text. I use something like this: typedef enum DataType { INTEGER, FLOAT_POINT, STRING } DataType ; typedef union DataValue { int v_int; float v_float; char* v_string; }DataValue; typedef struct DataNode { DataType type; DataValue value; }DataNode; void myfunct() { long long temp; DataNode inputData; inputData.type= read_some_input(&temp); switch(inputData.type) { case … Read more
To re-iterate, type-punning through unions is perfectly fine in C (but not in C++). In contrast, using pointer casts to do so violates C99 strict aliasing and is problematic because different types may have different alignment requirements and you could raise a SIGBUS if you do it wrong. With unions, this is never a problem. … Read more
In C: None. The famous “space-saving joke” #define struct union is almost not a joke. In C++98: Unions can only have POD members, non-union classes can have arbitrary members. In C++11: Unions can have arbitrary data members of object type (but not of reference type), but their use is more restricted that that of non-union … Read more
Designated initialisers come in two flavours: 1) It provides a quick way of initialising specific elements in an array: int foo[10] = { [3] = 1, [5] = 2 }; will set all elements to foo to 0, other than index 3 which will be set to 1 and index 5 which will be set … Read more
I had the same problem. For C89 the following is true: With C89-style initializers, structure members must be initialized in the order declared, and only the first member of a union can be initialized I found this explanation at: Initialization of structures and unions
In your example that you provided str will not be destructed. The standard states in [class.union]/2 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. If a union contains a non-static data … Read more
The fact that GCC is warning about unions doesn’t necessarily mean that unions don’t currently work. But here’s a slightly less simple example than yours: #include <stdio.h> struct B { int i1; int i2; }; union A { struct B b; double d; }; int main() { double d = 3.0; #ifdef USE_UNION ((union A*)&d)->b.i2 … Read more
The example attempts to illustrate the paragraph beforehand1 (emphasis mine): 6.5.2.3 ΒΆ6 One special guarantee is made in order to simplify the use of unions: if a union contains several structures that share a common initial sequence (see below), and if the union object currently contains one of these structures, it is permitted to inspect … Read more