LECTURE - 35
BASICS OF
STRUCTURES
Objectives
• Learn structure Basics
• Learn how to define a structure
• Learn how to declare variables of
structure type
• Learn how to access structure members
• Operations on structures
• Write Simple programs using structures.
CSE 101/102 Department of CSE 12/13/2018 2
Introduction
Syntax We’ve seen variables of simple data types, such as float,
char, and int.
Additional
Information
We saw derived data type, arrays to store group of
related data.
Do’s
Variables of such types represent one item of
Don’ts information: a height, an amount, a count, or group of
item with same data type: list[10] and so on.
Applications
Case studies
But, these basic types does not support the storage of
compound data.
Do it yourself
Eg. Student {name, address, age, sem, branch}
CSE 101/102 Department of CSE 12/13/2018 3
Introduction
Syntax
C/C++ provides facilities to define one’s own
Additional types (user defined).
Information
They may be a composite of basic types (int,
Do’s
double, etc) and other user-defined types.
Don’ts
Structures
Applications
Case studies
Do it yourself
CSE 101/102 Department of CSE 12/13/2018 4
Introduction
Syntax
Definition:
Additional
collection of one or more variables, possibly of
Information different types, grouped together under a single
name for convenient handling
Do’s
Don’ts • A structure type in C++ is called struct.
Applications
• A struct is heterogeneous in that it can be composed of
data of different types.
Case studies
• In contrast, array is homogeneous since it can contain
Do it yourself
only data of the same type.
CSE 101/102 Department of CSE 12/13/2018 5
Structures
Syntax
• Structures hold data that belong
Additional together.
Information
• Examples:
Do’s
Student record: student id, name, branch,
Don’ts
gender, start year, …
Bank account: account number, name,
Applications address, balance, …
Case studies
Address book: name, address, telephone
number, …
Do it yourself
• In database applications, structures are
called records.
CSE 101/102 Department of CSE 12/13/2018 6
Structures – Uses
Syntax
Structures are user-defined aggregate types.
Additional They assist program organisation by
Information
– Grouping logically related data, and giving this set of
Do’s variables a higher-level name and more abstract
representation.
Don’ts
– Enabling related variables to be manipulated as a
Applications
single unit rather than as separate entities.
– Reducing the number of parameters that need to be
Case studies passed between functions.
– Providing another means to return multiple values
Do it yourself
from a function.
CSE 101/102 Department of CSE 12/13/2018 7
Structures
• Individual components of a struct type
Syntax
Additional
Information
are called members (or fields).
Do’s
• Members can be of different types
(simple, array or struct).
Don’ts
• A struct is named as a whole while
Applications
individual members are named using field
Case studies
identifiers.
• Complex data structures can be formed
Do it yourself
by defining arrays of structs.
CSE 101/102 Department of CSE 12/13/2018 8
Structure Definition - Syntax
Syntax The general format of a structure definition is:
Additional
Information
struct tag_name
Do’s
{
Don’ts
data_type member1;
data_type member2;
Applications …
};
Case studies
Do it yourself
CSE 101/102 Department of CSE 12/13/2018 9
Structure Definition - Examples
Syntax
• Example:
Additional
Information
Do’s struct Date
Don’ts
{
int day; Members of the
Applications int month; structure Date
int year;
Case studies
} ;
Do it yourself
CSE 101/102 Department of CSE 12/13/2018 10
struct examples
Syntax • Example:
struct StudentInfo{
Additional
int Id;
Information
int age; The “StudentInfo”
char Gender; structure has 4 members
Do’s
double CGA; of different types.
Don’ts };
• Example:
Applications struct Employee{
char Name[15];
Case studies
char Address[30]; The “Employee”
int Age; structure has 6
Do it yourself
float Basic;
members
float DA;
float NetSalary;
}; CSE 101/102 Department of CSE 12/13/2018
struct examples
Syntax
• Example:
Additional struct BankAccount{
Information char Name[15];
int AcountNo;
Do’s double balance; The “BankAccount”
int day;
int month; structure has 6 members.
Don’ts
int year;
};
Applications
Case studies
• Example:
struct StudentRecord{
Do it yourself
char Name[15]; The “StudentRecord”
int Id;
char Dept[5]; structure has 4
char Gender; members.
};
CSE 101/102 Department of CSE 12/13/2018 12
Important Points Regarding
Structures
Syntax
Additional • Declaration of Structure reserves no space.
Information
Do’s • It is nothing but the “ Template / Map /
Shape ” of the structure .
Don’ts
Applications • Memory is created , very first time when a
variable of structure type is created /
Case studies Instance is created.
Do it yourself
CSE 101/102 Department of CSE 12/13/2018 13
Declaring Structure Variables
Syntax
• Declaration of a variable of struct type using struct tag
Additional name, after structure definition:
Information
<struct-type> <identifier_list>;
Do’s
• Example:
Don’ts StudentRecord Student1, Student2;
Applications
Name Name
Case studies Student1 Id Gender Id Gender Student2
Do it yourself Dept Dept
Student1 and Student2 are variables of StudentRecord type.
CSE 101/102 Department of CSE 12/13/2018 14
Declaring Structure Variables
Syntax
Declare them at the time of structure definition:
Additional
Information struct student
{
Do’s
int rollno;
Don’ts int age;
char name[10];
Applications float height;
}s1, s2, s3; /* Defines 3 variables of type student */
Case studies
Do it yourself Members of a structure themselves
are not variables. i.e. rollno alone does
not have any value or meaning.
CSE 101/102 Department of CSE 12/13/2018 15
Member or dot operator
Syntax
The link between member and a structure
Additional variable is established using the member
Information
operator ‘.’ which is also known as ‘dot
Do’s operator’
<struct-variable>.<member_name>
Don’ts
Applications
e.g.: student s1; // s1 is a variable of type
//student structure.
Case studies
s1. rollno;
Do it yourself
s1. age;
s1. name;
CSE 101/102 Department of CSE 12/13/2018 16
Ex: Member accessing using dot operator
Syntax
• Example:
Additional
StudentRecord Student1; //Student1 is a variable of type
Information //StudentRecord
strcpy(Student1.Name, "Chan Tai Man"); Student1
Do’s
Student1.Id = 12345;
Name
Don’ts
strcpy(Student1.Dept, "COMP");
Student1.gender = 'M'; Id Gender
Applications cout << "The student is ";
Dept
switch (Student1.gender){
Case studies case 'F': cout << "Ms. "; break;
Chan Tai Man
case 'M': cout << "Mr. "; break;
Do it yourself
} 12345 M
cout << Student1.Name << endl;
COMP
CSE 101/102 Department of CSE 12/13/2018 17
Syntax
Additional
Information
Do’s
Don’ts
Applications
Case studies
Do it yourself
CSE 101/102 Department of CSE 12/13/2018 18
Assigning values to members
Syntax
Different ways to assign values to the members of a structure:
Additional
Information
Assigning string:
Do’s
strcpy(s1.name, “Rama”);
struct student
Don’ts {
Assignment statement: int rollno;
int age;
Applications s1.rollno = 1335; char name[20];
float height;
s1.age = 18; }s1;
Case studies
s1.height = 5.8;
Do it yourself
Reading the values into memebers:
cin>>s1.name>>s1.age>>s1.rollno>>s1.height;
CSE 101/102 Department of CSE 12/13/2018 19
Structure: Example
Syntax struct Book { // definition
char title[20];
Additional
Information char author[15];
int pages;
Do’s
float price;
Don’ts };
void main( ){
Applications
Book b1, b2, b3;
Case studies
cout<<“Input values”;
cin>>b1.title>>b1.author>>b1.pages>>b1.price;
Do it yourself //output
cout<<b1.title<<b1.author<<b1.pages<<b1.price;
}
CSE 101/102 Department of CSE 12/13/2018 20
Structure Initialization Methods
There is one-to-one correspondence between
the members and their initializing values.
1. Without tag name.
main()
{
struct
{
int rollno;
int age;
}s1 ={20, 21};
…
}
CSE 101/102 Department of CSE 12/13/2018 21
Structure Initialization Methods
Syntax
Additional 2. Using tag name.
Information
Do’s
main ( )
Don’ts
{
struct Student
{
Applications
int rollno;
int age;
Case studies };
Student s1={20, 21};
Do it yourself Student s2={21, 21};
}
CSE 101/102 Department of CSE 12/13/2018 22
Structure initialization
Syntax
3. Using a tag name and defined outside the function.
Additional
Information
struct Student
Do’s
{
int rollno;
Don’ts int age;
} s1={20, 21};
Applications
main ( )
Case studies {
Student s2={21, 21};
Do it yourself
…
…
}
CSE 101/102 Department of CSE 12/13/2018 23
struct-to-struct assignment
Syntax
• The values contained in one struct type variable can be
assigned to another struct variable provided they both
Additional
Information belong to the same struct type.
Student1
• Example:
Do’s
StudentRecord Student1, Student2; Chan Tai Man
Don’ts strcpy(Student1.Name, "Chan Tai Man");
Student1.Id = 12345; 12345 M
Applications strcpy(Student1.Dept, "COMP"); COMP
Student1.gender = 'M';
Case studies Student2
Student2 = Student1;
Do it yourself
Chan Tai Man
12345 M
CSE 101/102 Department of CSE
COMP
12/13/2018 24
struct-to-struct assignment
- Example
Syntax
Additional
Information
Do’s
Don’ts
Applications
Case studies
Do it yourself
CSE 101/102 Department of CSE 12/13/2018 25
Comparison of structure variables
Syntax
If student1 and student2 belong to the same structure,
Additional then the following comparisons are valid:
Information
1. if(student1 .rollno == student2.rollno ) //comparison
Do’s of student1 and student2 with respect to the member rollno.
return 1 if they are equal,
Don’ts 0 otherwise.
Applications 2. if(student1 .rollno != student2 .rollno )
return 1 OR
if they are not equal,
Case studies 0 otherwise.
.
Do it yourself
Comparison of 2 structures should be done with respect to
any member variable.
No two structure variables can be compared directly!
CSE 101/102 Department of CSE 12/13/2018 26
Assigning and Comparing structure
variables : example
Syntax
struct Student{
int rollno;
Additional
Information
char name[20];
float marks;
Do’s };
void main(){
Don’ts
Student s1 = {111, “Rohan”, 72.50};
Applications Student s2 = {222, “Rishi”, 67.00};
If ((s1. rollno ==s2. rollno))
Case studies
cout<<“\nStudent1 and student2 are same\n”;
Do it yourself else
cout<<“\nStudent1 and student2 are NOT same\n”;
}
CSE 101/102 Department of CSE 12/13/2018 27
Operation on Individual members
Syntax • A member referred using the dot operator along with its
structure variable can be treated like any other variable name.
Additional • The precedence of the member operator is higher than
Information
all arithmetic and relational operators and therefore no
parentheses are required.
Do’s
if (s1.rollno == 111)
Don’ts
s1.marks += 10.0;
Applications float sum = s1.marks + s2.marks;
Case studies s1.marks++;
++s1.rollno; //applicable to numeric type members
Do it yourself
CSE 101/102 Department of CSE 12/13/2018 28
Summary
Syntax
• Structure Basics
Additional
Information
• Member accessing using dot operator
Do’s
Don’ts
• Assign and compare structure variables
Applications • Simple problems using structures
Case studies
Do it yourself
CSE 101/102 Department of CSE 12/13/2018 29