Most of us know why unions are used in C and how to use them.
As seen in my baseband processor code, Unions are used when we have to communicate between two modules and both of modules doesn't know which exact SIGNAL is to be communicated.
So union in that case act as wrapper.
Suppose you need to give to your friend some fruit, but he don't know which fruit it will be. He only knows you will give him just a single fruit.
So in that case FRUIT is like union, in which we define struct having fruit names (like bananana, apple).
And once your friend unwraps it, he will know what to do with it. Below is simple program for this
***********************************************************************
#include
typedef enum{
RED,
BLUE,
BLACK
}color;
typedef enum{
BANANA,
APPLE
}name;
typedef struct{
int numbers;
int price;
}banana;
typedef struct{
int price;
color col;
}apple;
typedef union{
banana b;
apple a;
}fruit;
typedef struct
{
fruit f;
name fruit_type;
}fruit_wrapper;
void send(fruit_wrapper);
int main()
{
apple d;
fruit f;
fruit_wrapper g;
d.price = 10;
d.col= RED;
f.a = d;
g.f = f;
g.fruit_type = APPLE;
send(g);
}
void send (fruit_wrapper f)
{
switch(f.fruit_type)
{
case (BANANA):
printf("fruit is banana");
break;
case (APPLE):
printf("fruit is apple");
break;
}
}
*****************************************************************************888
Also there is no need to have packed union, since it will have no effect. It will simply take biggest struct size.
Also during working, we found sometimes Compliers behave in diffrent way.
Ex : the below code resulted in crash in Visual Studio , but in gcc gave garbage value
typedef struct{
int a;
}che;
int main()
{
che var1;
printf("%d",che.var1);
}
// Since we are using struct variable without initializing, so to avoid compiler specific error , have variables initialize default values.
Union allocates the memory for the biggest structure. If you have assign value to any one structure , you can access the value from other structure also. the value will be what is stored in those bytes/bits.
Ex ( Below code is for VS )
#include
#pragma pack(1)
typedef struct{
short a;
int b;
}var1;
typedef struct{
int a;
short b;
}var2;
typedef union{
var1 t1;
var 2 t2;
}var3;
int main()
{
var 3 ki;
ki.t1.a=2;
ki.t1.b=1;
printf("%d",ki.t2.a);
printf("%d",ki.t2.b);
}
The answer wud be
65538 and 0
Explanation
Since both structures have one short and one int and also they are also packed, so 6 bytes will be reserved.
the byte allocation happens like below
the first member of structure occupies the LSB of total memory.
So for above example
ki.t1 has short and then int
so
1 1 1 1 (int bytes) 1 1 (short bytes) since value is 2 for short and 1 for int
0000 0000 0000 0000 0000 0000 0000 0001 (for int) <--> 0000 0000 0000 0010 ( this for short).
Now when we access ki.t2 , first int is there and then short ..so compiler divides memory as
0000 0000 0000 0000 (for short) <--> 0000 0000 0000 0001 0000 0000 0000 0010 ( for int).
Now convert into decimal 0 for short and 65538 for int
As seen in my baseband processor code, Unions are used when we have to communicate between two modules and both of modules doesn't know which exact SIGNAL is to be communicated.
So union in that case act as wrapper.
Suppose you need to give to your friend some fruit, but he don't know which fruit it will be. He only knows you will give him just a single fruit.
So in that case FRUIT is like union, in which we define struct having fruit names (like bananana, apple).
And once your friend unwraps it, he will know what to do with it. Below is simple program for this
***********************************************************************
#include
typedef enum{
RED,
BLUE,
BLACK
}color;
typedef enum{
BANANA,
APPLE
}name;
typedef struct{
int numbers;
int price;
}banana;
typedef struct{
int price;
color col;
}apple;
typedef union{
banana b;
apple a;
}fruit;
typedef struct
{
fruit f;
name fruit_type;
}fruit_wrapper;
void send(fruit_wrapper);
int main()
{
apple d;
fruit f;
fruit_wrapper g;
d.price = 10;
d.col= RED;
f.a = d;
g.f = f;
g.fruit_type = APPLE;
send(g);
}
void send (fruit_wrapper f)
{
switch(f.fruit_type)
{
case (BANANA):
printf("fruit is banana");
break;
case (APPLE):
printf("fruit is apple");
break;
}
}
*****************************************************************************888
Also there is no need to have packed union, since it will have no effect. It will simply take biggest struct size.
Also during working, we found sometimes Compliers behave in diffrent way.
Ex : the below code resulted in crash in Visual Studio , but in gcc gave garbage value
typedef struct{
int a;
}che;
int main()
{
che var1;
printf("%d",che.var1);
}
// Since we are using struct variable without initializing, so to avoid compiler specific error , have variables initialize default values.
Union allocates the memory for the biggest structure. If you have assign value to any one structure , you can access the value from other structure also. the value will be what is stored in those bytes/bits.
Ex ( Below code is for VS )
#include
#pragma pack(1)
typedef struct{
short a;
int b;
}var1;
typedef struct{
int a;
short b;
}var2;
typedef union{
var1 t1;
var 2 t2;
}var3;
int main()
{
var 3 ki;
ki.t1.a=2;
ki.t1.b=1;
printf("%d",ki.t2.a);
printf("%d",ki.t2.b);
}
The answer wud be
65538 and 0
Explanation
Since both structures have one short and one int and also they are also packed, so 6 bytes will be reserved.
the byte allocation happens like below
the first member of structure occupies the LSB of total memory.
So for above example
ki.t1 has short and then int
so
1 1 1 1 (int bytes) 1 1 (short bytes) since value is 2 for short and 1 for int
0000 0000 0000 0000 0000 0000 0000 0001 (for int) <--> 0000 0000 0000 0010 ( this for short).
Now when we access ki.t2 , first int is there and then short ..so compiler divides memory as
0000 0000 0000 0000 (for short) <--> 0000 0000 0000 0001 0000 0000 0000 0010 ( for int).
Now convert into decimal 0 for short and 65538 for int
No comments:
Post a Comment