May 11, 2021 C
A common body is a special type of data that allows you to store different data types in the same memory location. Y ou can define a common body with multiple members, but only one member can have a value at any one time. A common body provides an effective way to use the same memory location.
In order to define a common body, you must use union statements in a similar way to defining structures. T he union statement defines a new data type with multiple members. The union statement is in the following format:
union [union tag] { member definition; member definition; ... member definition; } [one or more union variables];
Union tag is optional, and each member definition is a standard variable definition, such as int i; O r float f; O r other valid variable definitions. Y ou can specify one or more common body variables, which is optional, before the last part at the end of the common body definition. Here's a common body type called Data with three members, i, f, and str:
union Data { int i; float f; char str[20]; } data;
Data-type variables can now store an integer, a float, or a string. T his means that a variable (the same memory location) can store multiple types of data. You can use any built-in or user-defined data type in a common body as needed.
The common body should consume enough memory to store the largest member of the common body. F or example, in the instance above, Data takes up 20 bytes of memory because strings take up the most space among members. The following example shows the total memory footprint of the common body above:
#include <stdio.h>
#include <string.h>
union Data
{
int i;
float f;
char str[20];
};
int main( )
{
union Data data;
printf( "Memory size occupied by data : %d\n", sizeof(data));
return 0;
}
When the above code is compiled and executed, it produces the following results:
Memory size occupied by data : 20
In order to access members of a common body, we use the member access operator (.). A member access operator is a period between the common body variable name and the member of the common body that we want to access. Y ou can use the union keyword to define variables of common body types. The following example demonstrates the use of a common body:
#include <stdio.h>
#include <string.h>
union Data
{
int i;
float f;
char str[20];
};
int main( )
{
union Data data;
data.i = 10;
data.f = 220.5;
strcpy( data.str, "C Programming");
printf( "data.i : %d\n", data.i);
printf( "data.f : %f\n", data.f);
printf( "data.str : %s\n", data.str);
return 0;
}
When the above code is compiled and executed, it produces the following results:
data.i : 1917853763 data.f : 4122360580327794860452759994368.000000 data.str : C Programming
Here, we can see that the values of the i and f members of the common body are corrupted because the values last assigned to the variable take up the memory position, which is why the str members are able to output well. Now let's look at the same instance again, this time we're using only one variable at the same time, which demonstrates the main purpose of using a common body:
#include <stdio.h>
#include <string.h>
union Data
{
int i;
float f;
char str[20];
};
int main( )
{
union Data data;
data.i = 10;
printf( "data.i : %d\n", data.i);
data.f = 220.5;
printf( "data.f : %f\n", data.f);
strcpy( data.str, "C Programming");
printf( "data.str : %s\n", data.str);
return 0;
}
When the above code is compiled and executed, it produces the following results:
data.i : 10 data.f : 220.500000 data.str : C Programming
Here, all members are output in good, because only one member is used at the same time.