C Programming UNIT 4.3 Structure and Union
C Programming UNIT 4.3 Structure and Union
Introduction
In C language, to store the integers, characters, decimal values, we have int, char, float, or
double data types already defined (also known as the primitive data types). Also, we have some
derived data types such as arrays and strings, to store similar types of data types elements
together. Still, the problem with arrays or strings is that they can only store variables of similar
data types, and the string can store only characters. What if we need to store two different data
types together in C for many objects? Like, there is a student variable that may have its name,
class, section, etc. So if we want to store all of its information, We can create different variables
for every variable like a character array to store name, integer variable to store the class, and a
character variable to store the section. But this solution is a little messy, C provides us with a
better neat and clean solution, i.e., Structure.
What is Structure
Structure is a user-defined data type that enables us to store the collection of different data
types. Each element of a structure is called a member.
The struct keyword is used to define the structure. Let's see the syntax to define the structure
in c.
struct structure_name
{
data_type member1;
data_type member2;
.
.
data_type memeberN;
};
The following image shows the memory allocation of the structure employee that is defined in
the above example.
Here, struct is the keyword; employee is the name of the structure; id, name, and salary are the
members or fields of the structure. Let's understand it by the diagram given below:
We can declare a variable for the structure so that we can access the member of the structure
easily. There are two ways to declare structure variable:
1. By struct keyword within main() function
2. By declaring a variable at the time of defining the structure.
1st way:
Let's see the example to declare the structure variable by struct keyword. It should be declared
within the main function.
struct employee
{ int id;
char name[50];
float salary;
};
The variables e1 and e2 can be used to access the values stored in the structure. Here, e1 and
e2 can be treated in the same way as the objects in C++ and Java.
2nd way:
Let's see another way to declare variable at the time of defining the structure.
struct employee
{ int id;
char name[50];
float salary;
}e1,e2;
Let's see another example of the structure in C language to store many employees information.
#include<stdio.h>
#include <string.h>
struct employee
{ int id;
char name[50];
float salary;
}e1,e2; //declaring e1 and e2 variables for structure
int main( )
{
//store first employee information
e1.id=101;
strcpy(e1.name, "Sonoo Jaiswal");//copying string into char array
e1.salary=56000;
Output:
employee 1 id : 101
employee 1 name : Sonoo Jaiswal
employee 1 salary : 56000.000000
employee 2 id : 102
employee 2 name : James Bond
employee 2 salary : 126000.000000
Let us see an example to understand how we can use an arrow operator to access structure
members using structures and pointers in C.
#include<stdio.h>
struct Student {
char name[30];
int age;
int roll_number;
};
int main() {
struct Student student_1;
struct Student *sp = &student_1;
printf ("\n Display the details of the student_1 using Structure Pointer\n");
printf ("\tName: %s\n", sp->name);
printf ("\tAge: %d\n", sp->age);
printf ("\tRoll Number: %d", sp->roll_number);
return 0;
}
Output
Accessing members of the structure using the membership operator on structure pointer
makes code more readable when compared to the other approach.
C Array of Structures
Output
Enter the name, id, and marks of student 1 James 90 90
Enter the name, id, and marks of student 2 Adoms 90 90
Enter the name, id, and marks of student 3 Nick 90 90
Printing the details....
James 90 90.000000
Adoms 90 90.000000
Nick 90 90.000000
In the above program, we have stored data of 3 students in the structure. However, the
complexity of the program will be increased if there are 20 students. In that case, we will have
to declare 20 different structure variables and store them one by one. This will always be tough
since we will have to declare a variable every time we add a student. Remembering the name
of all the variables is also a very tricky task. However, c enables us to declare an array of
structures by using which, we can avoid declaring the different structure variables; instead we
can make a collection containing all the structures that store the information of different
entities.
Array of Structures in C
An array of structres in C can be defined as the collection of multiple structures variables where
each variable contains information about different entities. The array of structures in C are used
to store information about multiple entities of different data types. The array of structures is
also known as the collection of structures.
Let's see an example of an array of structures that stores information of 5 students and prints
it.
#include<stdio.h>
#include <string.h>
struct student{
int rollno;
char name[10];
};
int main(){
int i;
struct student st[5];
printf("Enter Records of 5 students");
for(i=0;i<5;i++){
printf("\nEnter Rollno:");
scanf("%d",&st[i].rollno);
printf("\nEnter Name:");
scanf("%s",&st[i].name);
}
printf("\nStudent Information List:");
for(i=0;i<5;i++){
printf("\nRollno:%d, Name:%s",st[i].rollno,st[i].name);
}
return 0;
}
Output:
Enter Records of 5 students
Enter Rollno:1
Enter Name:Sonoo
Enter Rollno:2
Enter Name:Ratan
Enter Rollno:3
Enter Name:Vimal
Enter Rollno:4
Enter Name:James
Enter Rollno:5
Enter Name:Sarfraz
#include <stdio.h>
struct student {
char name[50];
int age;
};
return 0;
}
Output
Displaying information
Name: Bond
Age: 13
Here, a struct variable s1 of type struct student is created. The variable is passed to
the display() function using display(s1); statement.
UNION:
In C, a union is a user-defined data type that allows many different data types to be stored in
the same memory region. A union can have numerous members, but only one of them can
occupy the memory at any one moment. Unions allow developers to optimize memory usage
while declaring variables.
They are conceptually similar to structures. The syntax to define a union is also similar to that
of a structure. The only difference is in terms of storage. In a structure, each member has its
own storage location, whereas all members of a union use a single shared memory location,
which is equal to the size of its largest data member.
How to Define a Union?
union student
{
char name[50];
int roll number;
};