Published by: Nuru
Published date: 21 Jun 2021
Unions like structures contain members whose individual data types may differ from one another. However, the members that compose a union all share the same storage area within the computer's memory whereas each member within a structure is assigned its own unique storage area. Thus unions are used to observe memory. They are useful for applications involving multiple members, where values need not be assigned to all the members at any one time. Like structures union can be declared using the keyword union as follows:
union item
{
int m;
float p;
char c;
}
code;
This declares a variable code of type union item. It contains three members each with a different data type. However, we can use only one of them at a time. This is because if only one location is allocated for union variable irrespective of size. The compiler allocates a piece of storage that is large enough to access a union member we can use the same syntax that we use to access structure members. That is
code.m
code.p
code.c
are all valid member variables. During accessing we should make sure that we are accessing the member whose value is currently stored.
For example a statement such as
code.m=456;
code.p=456.78;
printf(“%d”,code.m);
typedef allows a new data type to be explicitly defined. Note that it does not actually create a new data class, but simply defines a new name for an existing type. This process helps in making machine-dependent code more portable since only the typedef statements need to changed. It can also aids in self-documenting a program by allowing the use of more descriptive names for the standard data types.
The general form of defining a new data type is:
typedef data type identifier;
Where identifier refers to the name(s) given to the data type. User-defined types obey the same scope rules as identifiers, so if one defined in a function, it is recognized only within that function. Few examples of type definitions are
typedef int age;
typedef float average;
typedef char string;
Where age symbolizes int, average symbolizes float,and ring symbolizes char.
They can be later used to declare variables as:
age child, adult;
average mark1, mark2;
string name[20];
Where child and adult are declared as integer variables, mark1 and mark2 are declared as floating point variables and name is declared as a character array variable. The typedef can also be used to define structures. The main advantage of type definition is that, you can create meaningful data type names for increasing the readability of the program. They also suggest the purpose of the data type names used in the program.
1. Keyword
The keyword ‘struct’ is used to define a structure whereas ‘union’ keyword is used to define a union.
2. Memory Allocation
Separate memory space is allotted for the members within a structure and members have different addresses that do not share memory. A union, on the other hand, shares the same memory space for all its members so shared memory location is allocated.
3. Member Access
A union stores a single value at a time for all its members making accessibility to only one member at a time. While multiple values can be stored in a structure so any member value can be accessed and retrieved at any time.
4. Size
The size of a structure is equal to the sum of the size of all members or more, whereas the size of a union is equal to the size of the largest size member.
5. Initialization
In a structure, several members can be initialized at once, while in a union, only the first member can be initialized with the value of its type.
6. Value
A structure can store different values of all the members and change in the value of one member will not have any effect on the values of other members. While a union stores the same value for all its members and change of the value of one member will affect the value of other.