Query for Structure and Union in C Language

Thread Starter

ankitdixit

Joined May 5, 2020
1
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?
 

Papabravo

Joined Feb 24, 2006
22,082
An array is a collection of members, each of which has the same data type.
A struct is a collection of members each of which can have a different data type.
A union is a way to access the same piece of data in multiple different ways e.g. as an array of bytes, an array of half words, and a word. The members of a union all refer to the same piece of memory, but in different ways.
 

WBahn

Joined Mar 31, 2012
32,823
I wouldn't say that a union is a way of accessing the same data in different ways, but this is based on a bit of semantics that equates "data" with "information". If I store the information 42 in a union as an 4-byte int, I can't then access that information as a float just because both types are defined as part of the union. A lot of people tend to think that this is the case, so it's an important semantic distinction.

I would say that a union is a way of accessing the same memory in different ways. When you use it as one member to store something there, what is getting stored in memory is a pattern of bits that is commensurate with storing the information using a particular representation -- storing the value 42 as a 4-byte two's complement integer, for instance. If we latter access that union using a member that is a float data type, then it is accessing the pattern of bits it found there and interpreting them according to the representation model used for the float data type, which is probably the IEEE-754 single-precision floating point representation. The value that results from doing so has no relationship to the value 42 that was initially stored there.

Unions are useful when we need a block of memory the communicate information between blocks of code but when representation of that information varies according to specific use. The producer and the consumer of the information have to have some means of agreeing what representation is currently being used, otherwise chaos generally ensues.
 

402DF855

Joined Feb 9, 2013
271
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.
 

WBahn

Joined Mar 31, 2012
32,823
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.
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.
 

MrChips

Joined Oct 2, 2009
34,807
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;
 

402DF855

Joined Feb 9, 2013
271
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
 

nsaspook

Joined Aug 27, 2009
16,321
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 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.
 
Last edited:

WBahn

Joined Mar 31, 2012
32,823
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;
It's been stated a few times that the embedded world uses unions pretty regularly.
 

402DF855

Joined Feb 9, 2013
271
are things the compiler should do
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.
 

nsaspook

Joined Aug 27, 2009
16,321
Ultimately the compiler does everything we tell it, using unions or not.
...
Could be badly misused.
Exactly, so let the compiler handle details unless it's necessary for clarity of expression with something tricky or efficiency.
That describes most of the C language and Algol 68 languages in general. :)

https://www.bell-labs.com/usr/dmr/www/chist.html
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.
 

nsaspook

Joined Aug 27, 2009
16,321
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.
MISRA C:2012 Rule 19.2 advises not to use unions, the shall not language has been removed.

The 2004 language has deviations.
Deviations are allowed as long as they are discussed and documented thoroughly. Data packing is something MISRA explicitly states to be an acceptable deviation is OK because most alternatives are worse.
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.
A typical deviation doc.
https://cypresssemiconductorco.gith...html/page_ble_section_indastry_standards.html
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.
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.

https://arxiv.org/pdf/2003.06893.pdf
 
Last edited:
Hi Ankit,
Here are the difference between Structure and Union:
A structure is a user-defined data type available in C that allows to combining data items of different kinds. Structures are used to represent a record.
A union is a special data type available in C that allows storing different data types in the same memory location. You can define a union with many members, but only one member can contain a value at any given time. Unions provide an efficient way of using the same memory location for multiple purposes.

There are a few similarities between Structure and Union:

  1. Both are user-defined data types used to store data of different types as a single unit.
  2. Their members can be objects of any type, including other structures and unions or arrays. A member can also consist of a bit field.
  3. Both structures and unions support only assignment = and sizeof operators. The two structures or unions in the assignment must have the same members and member types.
  4. A structure or a union can be passed by value to functions and returned by value by functions. The argument must have the same type as the function parameter. A structure or union is passed by value just like a scalar variable as a corresponding parameter.
  5. ‘.’ operator is used for accessing members.
MOD EDIT: Promotional link removed.
 
Last edited by a moderator:
Top