Programming in C: Unit IV: Structures and Union

Unions

Syntax with Example C Programs

Similar to structures, a union is a collection of variables of different data types. The only difference between a structure and a union is that in case of unions, you can only store information in one field at any one time.

UNIONS

Similar to structures, a union is a collection of variables of different data types. The only difference between a structure and a union is that in case of unions, you can only store information in one field at any one time.

To better understand a union, think of it as a chunk of memory that is used to store variables of different types. When a new value is assigned to a field, the existing data is replaced with the new data.

Thus unions are used to save memory. They are useful for applications that involve multiple members, where values need not be assigned to all the members at any one time.

Declaring a Union

The syntax for declaring a union is the same as that of declaring a structure. The only difference is that instead of using the keyword struct, the keyword union would be used. The syntax for union declaration can be given as

union union-name

{

data_type var-name;

data_type var-name;

………….

};

Programming Tip: Variable of a structure or a union can be declared at the time of structure/union definition by placing the variable name after the closing brace and before the semicolon.

Again, the typedef keyword can be used to simplify the declaration of union variables. The most important thing to remember about a union is that the size of a union is the size of its largest field. This is because sufficient number of bytes must be reserved to store the largest sized field.

Accessing a Member of a Union

A member of a union can be accessed using the same syntax as that of a structure. To access the fields of a union, use the dot operator (.), i.e., the union variable name followed by the dot operator followed by the member name.

Initializing Unions

A striking difference between a structure and a union is that in case of a union, the fields share the same memory space, so fresh data replaces any existing data. Look at the following code and observe the difference between a structure and union when their fields are to be initialized.

Programming Tip: It is an error to initialize any other union member except the first member.

#include <stdio.h>

typedef struct POINT1

{

int x, y;

};

typedef union POINT2

{

int x;

int y;

};

int main()

{

POINT1 P1 = {2,3};

// POINT2 P2 ={4, 5}; Illegal with union

POINT2 P2;

P2. x = 4;

P2. y = 5;

printf("\n The co-ordinates of P1 are %d and %d", P1.x, Pl.y);

printf("\n The co-ordinates of P2 are %d and %d", P2.x, P2.y);

return 0;

}

Output

The co-ordinates of P1 are 2 and 3

The co-ordinates of P2 are 5 and 5

In this code, POINT1 is a structure name and POINT2 is a union name. However, both the declarations are almost same (except the keywords - struct and union); in main(), you see a point of difference while initializing values. The fields of a union cannot be initialized all at once. Look at the output carefully. For the structure variable the output is fine but for the union variable the answer does not seem to be correct.

Programming Tip: The size of a union is equal to the size of its largest member.

To understand the concept of union, execute the following code. The code given below just re- arranges the printf statements. You will be surprised to see the result.

#include <stdio.h>

typedef struct POINT1

{

int x, y;

};

typedef union POINT2

{

int x;

int y;

};

int main()

POINT1 P1 = {2,3};

POINT2 P2;

printf("\n The co-ordinates of P1 are %d and %d", P1.x , Pl.y);

P2. x = 4;

printf("\n The x co-ordinate of P2 is %d", P2.x);

P2.y = 5;

printf("\n The y co-ordinate of P2 is %d", P2.y);

return 0;

}

Output

The co-ordinates of P1 are 2 and 3

The x co-ordinate of P2 is 4

The y co-ordinate of P2 is 5

Here although the output is correct, the data is still over-written in memory.

Programming in C: Unit IV: Structures and Union : Tag: : Syntax with Example C Programs - Unions