Hello Everyone, I am new here and I am preparing my self for my upcoming interview and I am confused about in structure and union, what's the exact difference between them. Can anyone suggest me the exact difference and which is better data type?
I would second this. I've seen a very few instances where unions were used in a rational and defensible way -- but I HAVE seen them. When I've used them is generally been to make demonstrating the difference between what is being represented and how it is being represented starkly obvious, but that's certain not what they were intended for.While it's important to know the difference between them, keep in mind that unions are used far less than structs. In fact, in my 30+ years of programming, I don't recall deploying a union myself, but I've worked on code that had them. I'd recommend getting familiar with bit fields in structs as they are often seen in embedded code.
_no_init volatile union
{
unsigned char P1OUT; /* Port 1 Output */
struct
{
unsigned char P0 : 1;
unsigned char P1 : 1;
unsigned char P2 : 1;
unsigned char P3 : 1;
unsigned char P4 : 1;
unsigned char P5 : 1;
unsigned char P6 : 1;
unsigned char P7 : 1;
} P1OUT_bit;
} @ 0x0021;
It's all syntactic sugar but in the hardware guy that programs domain Shifting and masking and casting are things the compiler should do when handling the memory abstraction of hardware bits with unions, not the programmer. Each has its place depending on the mental picture of what needs to be done as a physical task or a processing procedure.It's syntactic sugar, a matter of style, IME not used all that much. Shifting and masking and casting do pretty much the same thing and are usually clearer in their intent. YMMV
It's been stated a few times that the embedded world uses unions pretty regularly.On the contrary, we use union very frequently. Here is a typical example for referencing a port by bits or by byte:
C:_no_init volatile union { unsigned char P1OUT; /* Port 1 Output */ struct { unsigned char P0 : 1; unsigned char P1 : 1; unsigned char P2 : 1; unsigned char P3 : 1; unsigned char P4 : 1; unsigned char P5 : 1; unsigned char P6 : 1; unsigned char P7 : 1; } P1OUT_bit; } @ 0x0021;
Ultimately the compiler does everything we tell it, using unions or not. Currently I'm working with Atmel/Microchip's ARM softpack library of device drivers. Out of 207 header files describing the various peripheral registers just one uses a union. All the registers have masks and bit positions using #defines. Not saying it isn't used, just personally haven't run across it that much, and I can live without it. In a way it's even worse than over use of casting. Referring to the same area of memory as entirely different types and names, it's a little scary if you think about it. Could be badly misused.are things the compiler should do
Exactly, so let the compiler handle details unless it's necessary for clarity of expression with something tricky or efficiency.Ultimately the compiler does everything we tell it, using unions or not.
...
Could be badly misused.
The scheme of type composition adopted by C owes considerable debt to Algol 68, although it did not, perhaps, emerge in a form that Algol's adherents would approve of. The central notion I captured from Algol was a type structure based on atomic types (including structures), composed into arrays, pointers (references), and functions (procedures). Algol 68's concept of unions and casts also had an influence that appeared later.
MISRA C:2012 Rule 19.2 advises not to use unions, the shall not language has been removed.From JSF Coding Standards (note the reference to MISRA): http://www.stroustrup.com/JSF-AV-rules.pdf
AV Rule 153 (MISRA Rule 110, Revised) Unions shall not be used.
Rationale: Unions are not statically type-safe and are historically known to be a source of errors.
A typical deviation doc.It is recognized nonetheless that there are situations in which the careful use of unions is desirable in constructing an efficient implementation. In such situations, deviations to this rule are considered acceptable provided that all relevant implementation-defined behavior is documented. This might be achieved in practice by referencing the implementation section of the compiler manuals from the design documentation.
When used to change variable types (int/float) I agree, when used to build abstractions of very device dependent bit patterns ( serial communication message frames, etc ...) that are also are historically known to be a source of errors I don't.8.4 R Unions shall not be used. The use of deviations is acceptable for packing and unpacking of data, for example when sending and receiving messages, and implementing variant records provided that the variants are differentiated by a common field.