Structure & Union in C++

1,193 views 23 slides Jan 25, 2019
Slide 1
Slide 1 of 23
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23

About This Presentation

Structure & Union


Slide Content

STRUCTURE & UNION Ms. Davinder Kaur Assistant Professor Department of Computer Applications Chandigarh Group of Colleges, Landran

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.

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 ; }; };

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.

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;

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 (); }

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};

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 ; cout <<"Employee Salary is "<<emp1.salary; getch (); }

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.

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 (); }

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.

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 ; };

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 (); }

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

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” };

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 ].marks; } 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 (); }

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.

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; }

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 ; }

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 (); }

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 ; };

Example of Union #include< iostream.h > #include< conio.h > #include< string.h > union book { char name[20]; double price; } bk ; void main() { clrscr (); strcpy ( bk.name,"Communication -I"); cout <<"name of the book is "<<bk.name<< endl ; bk.price =234; cout <<"Price of book is "<< bk.price ; getch (); }

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.
Tags