Unit 9 Structure & Uniuon
Unit 9 Structure & Uniuon
Structure
Structure is the way to group the variable.
Structure is the collection of similar & disimmilar elements.
Defining structure means creating new datatype.
In structure, data is stored in the form of records.
Defining a Structure
Syntax
struct structure_name
{
// variable declaration here
(member variable)
};
Memory Consumption
No memory is consumed for a definition of structure.
For memory consumption, we need to declare the variable of type structure.
Array Of Structure
An array of structures in C can be defined as the collection of multiple structures variables
at a same time where each variable contains information about different entities.
The array of structures in C is used to store information about multiple entities of different
data types at a same time. The array of structures is also known as the collection of
structures.
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.
#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("\n Enter Rollno:");
scanf("%d" ,&st[i].rollno);
printf("\nEnter Name:");
scanf("%s" ,&st[i].name);
}
printf("\n Student Information List:");
for(i=0;i<5;i++){
printf("\n Rollno:%d, Name:%s" ,st[i].rollno, st[i].name);
}
return 0;
}
Nested Structure
One Structure inside another structure is known as the nested structure.
C provides us the feature of nesting one structure within another structure by using which,
complex data types are created.
1. By separate structure
2. By Embedded structure
1) Separate structure
Here, we create two structures, but one (dependent) structure should be used inside
another (main) structure as a member.
Example:-
struct Date
{
int dd;
int mm;
int yyyy;
};
struct Employee
{
int id;
char name[20];
struct Date doj;
}emp1;
doj is the variable of type Date. Here doj is used as a member in Employee structure.
In this way, we can use Date structure in many structures.
2) Embedded structure
The embedded structure enables us to declare the structure inside the structure. Hence,
it requires less line of codes but it cannot be used in multiple data structures.
Example:-
struct Employee
{
int id;
char name[20];
struct Date
{
int dd;
int mm;
int yyyy;
}doj;
}emp1;
emp1.doj.dd
emp1.doj.mm
emp1.doj.yyyy
#include <stdio.h>
#include <string.h>
struct Employee
{
int id;
char name[20];
struct Date
{
int dd;
int mm;
int yyyy;
}doj;
}e1;
int main( )
{
//storing employee information
e1.id=101;
strcpy(e1.name, "Bishal Patel"); //copying string into char array
e1.doj.dd=10;
e1.doj.mm=11;
e1.doj.yyyy=2014;
Consider the following example to pass the structure variable employee to a function
display() which is used to display the details of an employee.
Eg:-
#include<stdio.h>
struct address
{
char city[20];
int pin;
char phone[14];
};
struct employee
{
char name[20];
struct address add;
};
void display(struct employee);
void main ()
{
struct employee emp;
printf("Enter employee information?\n");
scanf("%s %s %d %s",emp.name,emp.add.city, &emp.add.pin, emp.add.phone);
display(emp);
}
void display(struct employee emp)
{
printf("Printing the details....\n");
printf("%s %s %d %s",emp.name,emp.add.city,emp.add.pin,emp.add.phone);
}
Output
Enter employee information?
Bishal
Birgunj
44300
9817206112
Printing the details….
Bishal Birgunj 44300 9817206112
Passing Array of Structure to a Function
In this C program, we are going to learn how to pass an array of structure to a user
define function? Here, we are an example, where we are passing a structure to a
function in C.
Given an array of structure and we have to pass it to the function in C.
Structure declaration
struct exam
{
int roll;
int marks;
char name[20];
};
Assigning values to the structure variables (array of structure variables) inside the main()
obj[0].marks = 35;
obj[0].roll = 10;
obj[1].marks = 75;
obj[1].roll = 11;
structfun(obj);
printf("\n");
printf("\nName is : %s",obj[1].name);
printf("\nRoll No. is : %d",obj[1].roll);
printf("\nMarks are : %d",obj[1].marks);
}
int main()
{
obj[0].marks = 35;
obj[0].roll = 10;
strcpy(obj[0].name , "Arjun kapoor");
obj[1].marks = 75;
obj[1].roll = 11;
strcpy(obj[1].name , "Balram chauhan");
structfun(obj);
return 0;
}
Pointer to Structure
The structure pointer points to the address of a memory block where the Structure is
being stored. we use a structure pointer which tells the address of a structure in memory
by pointing pointer variable ptr to the structure variable.
Program
#include <stdio.h>
#include <string.h>
struct Subject
{
char sub_name [30];
int sub_id;
char sub_duration [50];
char sub_type [50];
};
int main ()
{
struct Subject sub;
struct Subject *ptr;
ptr = & sub;
return 0;
}
Union
Union is the same as a structure the only difference is that in memory allocation.
Structure variable allocates total number of memory for all data types used in the
structure.
Union only allocates only one memory location for all data type.
Defining a Union
Syntax
union union_name
{
// variable declaration here
(member variable)
};
Eg:-
Eg:-
union abc
{
int a;
char b;
float c;
double d;
};
int main()
{
printf("Size of union abc is %d", sizeof(union abc));
return 0;
}
union abc
{
int a;
char b;
float c;
double d;
};
int main()
{
union abc *x;
printf("Size of a is %d", sizeof(x->a));
printf("Size of b is %d", sizeof(x->a));
printf("Size of c is %d", sizeof(x->a));
printf("Size of union abc is %d", sizeof(union abc));
return 0;
}
Enumeration
Enumeration (or enum) is a user defined data type in C. It consists of constant integrals
or integers that are given names by a user.
It is mainly used to assign names to integral constants, the names make a program easy
to read and maintain.