0% found this document useful (0 votes)
10 views17 pages

Lecture-27 (25-11-2023)

The document explains the concept of structures in computer programming, highlighting their role in organizing related data items. It covers structure definition, member access, initialization, assignment, and the use of arrays of structures. Examples are provided to illustrate how to define and manipulate structures in C++.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views17 pages

Lecture-27 (25-11-2023)

The document explains the concept of structures in computer programming, highlighting their role in organizing related data items. It covers structure definition, member access, initialization, assignment, and the use of arrays of structures. Examples are provided to illustrate how to define and manipulate structures in C++.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Problem Solving and

MA144:
Computer Programming

Lecture-27
Structures-1
Structure
What is a Structure?
• It is a convenient construct for representing a group of
logically related data items.
Examples:
Student name, roll number, and marks.
Real part and imaginary part of a complex number.
• Structures help in organizing complex data in a more
meaningful way.
• The individual structure elements are called members.
• This is first user-defined data-type.
Structure Definition
struct name_of_structure
{ data_type member_name1;
data_type member_name2;
data_type member_name3;
};
Example

struct student
{ char name[30];
int rollNo;
char grade;
};
struct is the required keyword
• Do not forget the ; at the end of the structure definition
• The individual members can be ordinary variables,
pointers, arrays, or other structures (any data-type)
• The member names within a particular structure must
be distinct from one another
• A member name can be the same as the name of a
variable defined outside of the structure
Defining structure variables
#include<iostream> #include<iostream>
using namespace std; using namespace std;
struct student struct student
{ char name[30]; { char name[30];
int rollNo; int rollNo;
char grade; char grade;
}; }x,y;
int main() int main()
{ student x,y; {

return 0; return 0;
} }
Accessing structure members
• The members of a structure are accessed individually,
as separate entities.
• A structure member can be accessed by writing

<structure-variable-name>.<member-name>

Dot operator is used to access members of a structure,


through a structure-type variable
struct student
{ char name[30];
int rollNo; y.name;
char grade; y.rollNo;
}; y.grade;

int main() return 0;


{ student x,y; }

x.name;
x.rollNo;
x.grade;
Structure Initialization
Structure variables may be initialized following similar rules of an array.
The values are provided within braces separated by commas.
#include<iostream>
#include<string>
using namespace std;
struct student
{ int roll;
string name;
};
int main()
{ student x={24,"hai"};
cout<<"\n student x details: "<<endl;
cout<<" Roll: "<<x.roll<<endl;
cout<<" student name: "<<x.name;
return 0;}
Run-time initialization
#include<iostream>
#include<string>
using namespace std;
struct student
{ int roll;
string name;
}x;
int main()
{ cout<<"enter roll of x: ";
cin>>x.roll;
cout<<" enter name of student x: ";
cin>>x.name;
cout<<"\n student x details: "<<endl;
cout<<" Roll: "<<x.roll<<endl;
cout<<" student name: "<<x.name;
return 0;}
Assignment of Structure Variables
• A structure variable can be directly assigned to another
variable of the same type.
• All the individual members get assigned / copied.
• Structures can be copied directly – even if they contain
arrays
• But, two structure variables cannot be compared
for equality or inequality

int a[5] = { 10, 20, 30, 40, 50 };


int b[5]; b = a;
This is not allowed
struct student
{ int roll;
char name[30];
}x,y;
int main()
{ cout<<"enter roll of x: ";
cin>>x.roll;
cout<<" enter name of student x: ";
cin>>x.name;
y=x;
cout<<"\n student x details: "<<endl;
cout<<" Roll: "<<y.roll<<endl;
cout<<" student name: "<<y.name;
return 0;
}
Array of Structures
Once a structure data-type has been defined, we can
declare an array of structures.
struct ECE
{
int rollNo;
char name[20];
int marks;
};
ECE student[55];

The individual members can be accessed as:


student[k].rollNo roll number of the kth student
student[k].marks marks of the kth student
student[k].name name of the kth student
student[k].name[i] i th character in the name of
kth student
#include<iostream>
using namespace std;
struct complex
{ int real; Addition of Complex numbers
int img;
};
int main()
{ complex z[2],w;
int i;
for(i=0;i<2;i++)
{
cout<<"enter complext number "<<i+1<<": ";
cin>>z[i].real>>z[i].img;
}
w.real=z[0].real+z[1].real;
w.img=z[0].img+z[1].img;
cout<<"sum of "<<z[0].real<<"+i"
<<z[0].img<<" and "<<z[1].real<<"+i"
<<z[1].img<<" is "<<endl;
cout<<w.real<<"+i"<<w.img;
return 0;
}

You might also like