Structure and Union
Structure and Union
To store the data of students like student name, age, address, id etc. One way of doing this would
be creating a different variable for each attribute, however when you need to store the data of
multiple students then in that case, you would need to create these several variables again for
each student. This is such a big headache to store data in this way.
To solve above problem, we need a single variable, which can store a group of data of dissimilar
type. Structures and Unions are used to solve the above mentioned problem.
Structure:
A structure is a user-defined data type available in C that allows to combining data items of
different kinds. Structures are used to represent a record.
struct struct_name {
DataType member1_name;
DataType member2_name;
DataType member3_name;
…
} var_name;
Example of Structure in C
#include <stdio.h>
/* Created a structure here. The name of the structure is
* StudentData.
*/
struct StudentData{
char stu_name[20];
int stu_id;
int stu_age;
};
void main()
{
/* student is the variable of structure StudentData*/
struct StudentData student;
Union
Union is a user-defined data type, just like a structure. Union combines objects of different types
and sizes together. The union variable allocates the memory space equal to the space to hold the
largest variable of union. It allows varying types of objects to share the same location.
Syntax of Declaring Union
union [name of union]
{
type member1;
type member2;
type member3;
};
• Union is declared using the "union" keyword and name of union.
• member1, member2, member 3 are individual members of union.
• The body part is terminated with a semicolon (;).
Example of Union in C Programming
#include <stdio.h>
union item
{
int x;
float y;
char ch;
};
void main( )
{
union item it;
it.x = 12;
it.y = 20.2;
it.ch = 'a';
printf("x=%d\n", it.x);
printf("y=%f\n", it.y);
printf("ch=%c\n", it.ch);
}
Output:
x=1101109601
y=224.6878
ch=a
In the above program, you can see that the values of x and y gets corrupted. Only variable ch
prints the expected result. It is because, in union, the memory location is shared among all
member data types.
#include <stdio.h>
struct student {
char firstName[50];
int roll;
float marks;
} s[10];
void main() {
int i,n;
printf(“Enter number of students\n”);
scanf(“%d”,&n);
printf("Enter information of students:\n");
// storing information
for (i = 0; i < n; ++i) {
s[i].roll = i + 1;
printf("\nfor roll number%d,\n", s[i].roll);
printf("Enter first name: ");
scanf("%s", s[i].firstName);
printf("Enter marks: ");
scanf("%f", &s[i].marks);
}
printf("Displaying Information:\n\n");
// displaying information
for (i = 0; i < n; ++i) {
printf("\nRoll number: %d\n", i + 1);
printf("First name: ");
puts(s[i].firstName);
printf("Marks: %.1f", s[i].marks);
printf("\n");
}
}
Output
Enter number of students
2
Enter information of students:
For roll number1,
Enter name: Tom
Enter marks: 98
For roll number2,
Enter name: Jerry
Enter marks: 89
Displaying Information:
Roll number: 1
Name: Tom
Marks: 98
Roll number: 2
Name: Jerry
Marks: 89
In this program, a structure student is created. The structure has three members: name (string),
roll (integer) and marks (float).
Then, we created an array of structures s having 2 elements to store information of 2 students.
Using a for loop, the program takes the information of 2 students from the user and stores it in
the array of structure. Then using another for loop, the information entered by the user is
displayed on the screen.
NOTE: Refer Class work for more examples.
Difference between Structures and Unions:
Structures Unions
A structure is a user-defined data type available A union is a special data type available in C that
in C that allows to combining data items of allows storing different data types in the same
different kinds. Structures are used to represent memory location.
a record.
struct keyword is used to define a structure. union keyword is used to define a union.
Syntax: union u_name{
struct s_name{ type element1;
type element1; type element2;
type element2; .
. .
. } variable1, variable2, ...;
} variable1, variable2, ...;
Every member within structure is assigned a In union, a memory location is shared by all
unique memory location. the data members.
Changing the value of one data member will Changing the value of one data member will
not affect other data members in structure. change the value of other data members in
union.
It enables to initialize several members at It enables you to initialize only the first
once. member of union.
The total size of the structure is the sum of the The total size of the union is the size of the
size of every data member. largest data member.
Provide single way to view each memory Provide multiple ways to view same memory
location. location.
Example: Example:
struct STUDENT union STUDENT
{ {
Char name[20]; Char name[20];
int sem; int sem;
}; };
Size of STUDENT Structure is 22Bytes Size of STUDENT union is 20Bytes.