For example,
union Sample
{
float x;
int a;
char ch;
};
Now you can create a union variable of type union
Sample
as follows:
Union Sample
one
;
Here,
one
is a union variable. Yet another way of achieving this is the following:
union Sample
{
float x;
int a;
char ch;
}one;
Like structures, union members are accessed exactly the same way that structure members are
accessed, using a dot (.) operator. The individual union members are accessed as follows:
one.x = 3.1417;
one.a = 80;
one.ch = ‘R’;
Please note that a union variable may contain only one meaningful value at a time and it is
incorrect to store something as one type and then extract it as another. It means that union
permits a part of memory to be treated as a variable of one type on one occasion and as a
Do'stlaringiz bilan baham: |