C Tutorial
In C programming, we usally deal with collection of ints, chars and floats rather than isolated entities.
Example an entity book is a collection of things like a title, an author, total pages, date of publication and price etc..
Union is user defined data type which combines dissimilar data types into an entity and similar like c structure and difference is memory allocation for the entity.
Union size is the size of the largest size of a element.
In union, we need to access only one element at a time because remaining elements may be corrupted.
union <name> { <data-type> member1; <data-type> member2; ... <data-type> membern; };
union <name> <variable-name>;
Let us consider the union entity student,
union student { char name[100]; int id, mark; };
here size of the union is the same as size of name member because name member size is the largest than other union student elements id and mark.
Sample c program which defines the union for student entity, assigns value for each attributes in the entity and shows values are corrupted issue when reading all elements at a time.
#include<stdio.h> #include<string.h> union student { char name[100]; int id, mark; }; void main() { union student stu1; /*printf("\nEnter id: "); scanf("%d", &stu1.id); printf("Enter name: "); scanf(" %s", stu1.name); printf("Enter mark: "); scanf("%d", &stu1.mark); */ stu1.id=32; strcpy(stu1.str, "Y"); stu1.mark =87; printf("\nStudent details\n"); printf("------------------\n"); printf("id: %d\n", stu1.id); printf("name: %s \n", stu1.name); printf("mark: %d\n", stu1.mark); printf("size of stu1: %d\n", sizeof(stu1)); }
Output:
$ cc union_test.c $ ./a.out Student details ------------------ id: 87 name: W mark: 87 size of stu1: 100
Here each member value is corrupted since sharing the same storage area for the enitre entity. so to get all the elements correctly, we need to update the code like each printf statements.
Need to update and read union elements one at a time otherwise may corrupt the values.
#include<stdio.h> #include<string.h> union student { char name[100]; int id, mark; }; void main() { union student stu1; printf("\nStudent details\n"); printf("------------------\n"); stu1.id=32; printf("id: %d\n", stu1.id); strcpy(stu1.str, "codingpointer.com"); printf("name: %s \n", stu1.name); stu1.mark =87; printf("mark: %d\n", stu1.mark); printf("size of stu1: %d\n", sizeof(stu1)); }
Output:
$ cc union_test.c $ ./a.out Student details ------------------ id: 32 name: codingpointer.com mark: 87 size of stu1: 100
Structure allocates memory for each member but union allocates memory based on largest size of element and share same memory for each members.
Only one element is accessible at a time because other elements may be corrupted when using union, all elements accessible in structure at a time.
#include<stdio.h> union student1 { char name[100]; int id, mark; }; struct student2 { char name[100]; int id, mark; }; void main() { union student1 stu1; struct student2 stu2; printf("size of stu1: %d\n", sizeof(stu1)); printf("size of stu2: %d\n", sizeof(stu2)); }
Output:
$ cc union_test.c $ ./a.out size of stu1: 100 size of stu2: 108
name size is 100 (1 byte for each character, so 100 bytes for 100 characters) and id size is 4 (for integer 4 bytes) and mark is 4 (for integer 4 bytes).
C Tutorial
Privacy Policy | Copyright2020 - All Rights Reserved. | Contact us
| Report website issues in Github
| Facebook page
| Google+ page