Answered # unions

deceptikon 1,775 Need some help with this Array. I am trying to get the sum of the even numbers and the sum of the odd numbers using a for each loop. I know the answers to what I am trying to achive are sum of even = 84 and the sum of ...

0

where is union required?

A union is never *required*, but it can simplify certain tasks such as type punning. An example is from the C standard library implementation in my signature. I use a union for punning between an IEEE double, its 64-bit representation, and a breakdown of the key components (specified by bitfields):

```
typedef union _real8 {
unsigned long long ivalue;
double fvalue;
struct {
unsigned long long mantissa : 52;
unsigned long long exponent : 11;
unsigned long long sign : 1;
} parts;
} _real8_t;
```

This saves me from having to use the bitwise operators to break down the value manually such as in this helper function:

```
/*
@description:
Implementation helper for the fpclassify macro for double/long double.
*/
int _fpclassifyd(double value)
{
_real8_t fpv;
fpv.fvalue = value;
if (fpv.parts.exponent == 0)
return fpv.parts.mantissa == 0 ? FP_ZERO : FP_SUBNORMAL;
else if (fpv.parts.exponent == 0x7FF)
return fpv.parts.mantissa == 0 ? FP_INFINITE : FP_NAN;
return FP_NORMAL;
}
```

The alternative would be much uglier and harder to infer the intended meaning of the code.

please explain with an example where having union is a better choice than any data structure.

This question is confusing, and I suspect you're using "data structure" to mean C's struct. Keep in mind that "data structure" is used elsewhere in computer science to mean a more general way of storing and accessing data (eg. linked lists, trees, stacks, queues).

This question has already been answered. Start a new discussion instead.

Recommended Articles

When I execute this progammatically, I get a table with row heights much larger than when I do this manually.

Note : Sel is the Word.Selection object and the Clipboard contains an Excel Table.

```
public void AddClipboard()
{
Sel.PasteExcelTable(false,false, false);
var t = Sel.Tables[Sel.Tables.Count];
t.AutoFitBehavior(Word.WdAutoFitBehavior.wdAutoFitContent);
}
```

the function that I created to find the ...