Structure & Union both are users define data types. But the difference between structure and union is in case of structure each and every member can take its own memory location. For example, to maintain the employee details like code, name define a structure as follows
{
int code;
char name[15];
};
In the above, the size of the structure is 17 bytes, how we will see int data type size in C-Language is 2 bytes and char data type is 1 byte. For the array name, the size is 15. So the size of name is 15 bytes. Totally it 2 + 15 = 17 bytes.
In case of the union, it will take the common memory location which is highest one in members. For example if we consider same example
union employee
{
int code;
char name[15];
};
In the above, the highest size member is name with the size 15 bytes. If this is the situation where code data is stored.
Here my question is what is the proper example to highlight the importance of the union. This we will see in the next post. See you tomorrow.
Comments
Post a Comment