1. STRUCTURE &
UNION
Ms. Davinder Kaur
Assistant Professor
Department of Computer Applications
Chandigarh Group of Colleges, Landran
2. STRUCTURE
A Structure is a collection of related data items which can be of different types held
together in a single unit.
All the elements of structure are stored at contiguous memory locations.
The data items enclosed within a structure are known as its members which can be
either of int, float, char etc.
3. Structure Declaration
A structure declaration specifies the grouping of
variables of different types in a single unit.
The syntax for declaring a structure in C++ is-
Example of Structure Declaration is-
struct struct_name struct student
{ {
data_type member1; int rollno;
data_type member2; char name[20];
…….. float marks;
data_type membern; };
};
4. Structure Definition
The structure definition creates structure variables and allocates
storage space for them.
The individual members of the structure variables are stored in
contiguous memory locations.
For example-
struct student s1,s2;
Or student s1,s2;
struct student
{
int rollno;
char name[20];
float marks;
}s1,s2;
Here s1,s2 are two structure variables of type student.
5. Accessing Structure Members
The members can be accessed using a dot operator(.)
or an arrow operator(->).
The use of extraction operator(>>) and insertion
operator(<<) to input and display the members of
structure variable s1.
cin>>s1.rollno>>s1.name>>s1.marks;
cout<<s1.rollno<<s1.name<<s1.marks;
6. Example of Accessing members-
#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
struct student
{
int rollno;
char name[20];
float marks;
};
void main()
{
struct student s1; clrscr();
cout<<"Enter roll no, name,marks"; cin>>s1.rollno>>s1.name>>s1.marks;
cout<<endl; cout<<"Roll no is:"<<s1.rollno<<endl;
cout<<"Name of student is:"<<s1.name<<endl;
cout<<"Marks are:"<<setprecision(2)<<s1.marks<<endl;
getch();
}
7. Structure Initialization
A structure variable is initialized with values that are to be assigned to its members
within the braces.
Example-
struct employee
{
int emp_no;
char emp_name[20];
int dept_id;
double salary;
};
Defining and initialize structure variable emp1 of type employee as follow:
employee emp1={101,”Aman”,1125,20000};
8. Example of Structure Initialization
#include<iostream.h>
#include<conio.h>
struct employee
{
int emp_no;
char emp_name[20];
int emp_id;
double salary;
};
void main()
{
struct employee emp1={101,"Davinder",1125,20000};
clrscr();
cout<<"Employee Details are-"<<endl;
cout<<"Employee number is "<<emp1.emp_no<<endl;
cout<<"Employee name is "<<emp1.emp_name<<endl;
cout<<"Employee Id is "<<emp1.emp_id<<endl;
9. Structure Assignment
The value of one structure variable is assigned to another
variable of same type using assignment statement. If s1 and s2
are structure variable of type student then the statement
s2=s1;
assign value of structure variable s1 to s2.
10. Example of Structure Assignment
#include<iostream.h>
#include<conio.h>
struct student
{
int rollno;
char name[20];
float marks;
};
void main()
{
struct student s1={101,"Simran",50.677654};
struct student s2;
clrscr();
s2=s1;
cout<<"Roll number is "<<s2.rollno<<endl;
cout<<"Name is "<<s2.name<<endl;
cout<<"Marks are "<<s2.marks<<endl;
getch();
}
11. Nesting of Structure (Structure
within Structure)
When a structure contains another structure, it is
called nested structure.
In other words, a member of a structure is a
variable of another structure. This process in
which a structure can appear within another
structure is known as nesting of structure.
12. Syntax for structure within
structure or nested structure
struct structure_name1
{
datatype variable_name;
datatype varaiable_name;
};
struct structure_name2
{
datatype variable_name;
datatype variable_name;
struct structure_name1 obj;
};
13. Example of Nesting Structure#include<iostream.h>
#include<conio.h>
struct date
{
int day;
int month;
int year;
};
struct employee
{
int emp_code;
char emp_name[20];
int dept_id;
float sal;
date doj;
};
void main()
{
employee emp1;
clrscr();
cout<<"Enter employee code, name of employee ";
cin>>emp1.emp_code>>emp1.emp_name;
cout<<"Enter dept id, salary";
cin>>emp1.dept_id>>emp1.sal;
cout<<"Enter date,month and year of joining";
cin>>emp1.doj.day>>emp1.doj.month>>emp1.doj.year;
cout<<endl;
cout<<"Displaying information of Employee is:"<<endl;
cout<<"Employee Code is:"<<emp1.emp_code<<endl;
cout<<"Name of Employee
is:"<<emp1.emp_name<<endl;
cout<<"Department id is:"<<emp1.dept_id<<endl;
cout<<"Salary is:"<<emp1.sal<<endl;
cout<<"Joining date is:"<<emp1.doj.day<<"-
"<<emp1.doj.month<<"-"<<emp1.doj.year;
getch();
}
14. Array of Structure
Array of structure refers to an array in which each array
element is a structure variable of same type.
For Example-
struct student
{
int rollno;
char name[20];
float marks;
}s[10]; //array of structure
15. Initialization of Array of Structure
An array of structures can be assigned some initial values just as any other build in
types.
For Example-
struct student
{
int rollno;
char name[20];
};
………….
………..
Student s[3]={101,”Aman”,
102,”Vicky”,
103,”Rishi”
};
16. Example of Array of Structure
#include<iostream.h>
#include<conio.h>
struct student
{
int rollno;
char name[20];
float marks;
};
void main()
{
student s[5];
int n;
clrscr();
cout<<"Enter number of students";
cin>>n;
for(int i=0;i<n;i++)
{
cout<<"Enter roll no,name, marks";
cin>>s[i].rollno>>s[i].name>>s[i].ma
rks;
}
cout<<"Displaying Students Information
"<<endl;
for(i=0;i<n;i++)
{
cout<<"Rollno is "<<s[i].rollno<<endl;
cout<<"Name is "<<s[i].name<<endl;
cout<<"Marks is "<<s[i].marks<<endl;
}
getch();
}
17. Structures & Functions
Individual members of a structure can be passed to a
function as argument in the function call. This method
of passing individual member is same as that of passing
variable of any primitive type.
18. Example of Passing Structure to function
#include<iostream.h>
#include<conio.h>
struct employee
{
int emp_code;
char name[20];
double sal;
};
void main()
{
void display(employee);
//Function Declaration
employee emp1={101,"Ritu",12000};
clrscr();
cout<<"Details of Employee are "<<endl;
display(emp1);//Function Call
getch();
}
void display(employee e)
{
cout<<"Code of employee
is:"<<e.emp_code<<endl;
cout<<"name of Employee is
"<<e.name<<endl;
cout<<"Salary is "<<e.sal;
}
19. Structure With Pointers
Like pointers to int, char and other data-types, pointers also pointing to
structures. These pointers are called structure pointers.
Syntax is-
struct structure_name
{
data-type member-1;
data-type member-1;
data-type member-1;
data-type member-1;
};
int main()
{
struct structure_name *ptr;
}
20. Example of structure with pointer
#include<iostream.h>
#include<conio.h>
struct student
{
char name[20];
int rollno;
};
void main()
{
struct student s={"Raman",123};
struct student *ptr;
clrscr();
ptr=&s;
cout<<"name and roll no. of student is
"<<s.name<<"t"<<s.rollno;
cout<<"nname and roll no is "<<ptr-
>name<<"t"<<ptr->rollno;
getch();
}
21. Union
A union is a user-defined data type like structure. The union
groups logically related variables into single unit.
The union data type allocate the space equal to space need to hold
the largest data member of union.
The union allows different types of variable to share same space in
memory.
Syntax is-
union <union_name>
{
datatype variable_name;
datatype variable_name;
};
23. Difference between Structure & Union
Structure occupies memory for all its members whereas Union will
not take memory for all its members. It will take the memory
occupied by the highest memory occupying member.
In union, one block is used by all members of the union but in case
of structure, each member has its own memory space.
Union is the best environment where memory is limited as it
shares the allocated memory. But structure cannot be implemented
in shared memory.
In union, only one member can be assigned a value at a time. But
in structures, all the members can be assigned values at a time.
In structure, value assigned to one member cannot cause change in
other members, where as in union it causes change in values of
other members.