Data Structures Notes
Data Structures Notes
Data Structures Notes
10. Once the structure is defined, you can declare variables of that
structure type. For example:
Student s1; // Declaration of a structure variable
11. To access the members of a structure variable, we use the dot
operator (.) followed by the member name.
12. For example:
s1.rollNumber = 1001; // Accessing and
assigning value to the rollNumber member
s1.name = "John Doe"; // Accessing and
assigning value to the name member
s1.age = 20; // Accessing and assigning value
to the age member
13. Here, we access the members of the s1 variable and assign values
to them using the dot operator.
Example:
#include <iostream>
using namespace std;
struct Student {
int rollNumber;
string name;
int age;
};
int main() {
Student s1;
s1.rollNumber = 1001;
s1.name = "John Doe";
s1.age = 20;
return 0;
}
struct Student {
int rollNumber;
string name;
int age;
};
int main() {
Student s1;
s1.rollNumber = 1001;
s1.name = "John Doe";
s1.age = 20;
return 0;
}
Array of Structures
Example:
struct Student {
int rollNumber;
string name;
int age;
};
int main() {
Student students[5]; // Creating an array of
5 Student structures
students[1].rollNumber = 1002;
students[1].name = "Jane Smith";
students[1].age = 19;
// ...
return 0;
}
In the above example, we create an array of 5 Student structures named
students. Each element of the array can hold information about a
student, including the rollNumber, name, and age.
Example:
#include <iostream>
using namespace std;
struct Student {
int rollNumber;
string name;
int age;
};
int main() {
int MAX_STUDENTS;
cin>>MAX_STUDENTS;
Student students[MAX_STUDENTS];
return 0;
}
After that, another loop is used to output the student details from the
array of structures.
Example:
#include <iostream>
using namespace std;
struct Point {
int x;
int y;
};
return 0;
}
In the main function, we create a Point p1 with initial coordinates (3, 5).
We then call the displayPoint function to display its coordinates.
Pointer to Structures
18. A pointer to a structure holds the memory address of the
structure variable.
19. The pointer is declared using the structure name followed by an
asterisk (*) and the pointer variable name.
20. To access the members of a structure through a pointer, the
arrow operator (->) is used.
Example:
struct Person {
string name;
int age;
};
int main() {
Person p1 = {"John Doe", 25};
Person* ptr = &p1;
return 0;
}
#include <iostream>
using namespace std;
struct Person {
string name;
int age;
};
int main() {
Person p1 = {"John Doe", 25};
Person* ptr = &p1;
cout << "Name: " << ptr->name << endl;
cout << "Age: " << ptr->age << endl;
cout << "Name: " << (*ptr).name << endl;
cout << "Age: " << (*ptr).age << endl;
return 0;
}
Example:
struct Point {
int x;
int y;
};
int main() {
Point* ptr = new Point; // Dynamic
allocation of Point structure
cout << "Coordinates: (" << ptr->x << ", " <<
ptr->y << ")" << endl;
return 0;
}
Pointers in structures have several uses
Enumeration
Enumeration is a user-defined data type that consists of a set of named
values called enumerators. It allows programmers to define a list of
named constants, making the code more readable and maintainable.
enum EnumName {
Enumerator1,
Enumerator2,
// ...
};
Example:
#include <iostream>
using namespace std;
enum Day {
Monday,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday,
Sunday
};
int main() {
Day today = Wednesday;
cout << "Today is ";
switch (today) {
case Monday:
cout << "Monday";
break;
case Tuesday:
cout << "Tuesday";
break;
case Wednesday:
cout << "Wednesday";
break;
case Thursday:
cout << "Thursday";
break;
case Friday:
cout << "Friday";
break;
case Saturday:
cout << "Saturday";
break;
case Sunday:
cout << "Sunday";
break;
}
return 0;
}
Example:
#include <iostream>
using namespace std;
enum Color {
Red = 1,
Green = 2,
Blue = 4
};
int main() {
Color c1 = Red;
Color c2 = Green;
return 0;
}
Class Codes
#include <iostream>
using namespace std;
struct Student{
int rollNumber;
string Name;
int age;
};
int main() {
Student std1 = {1002332, "Aveek", 27};
Student std2;
std2.rollNumber= 12321031;
std2.Name = "Akshay";
std2.age = 21;
Student std3;
cin>>std3.rollNumber>>std2.Name>>std2.age;
return 0;
}
Example
#include <iostream>
using namespace std;
struct Student{
int rollNumber;
string Name;
int age;
};
int main() {
Student std1 = {1002332, "Aveek", 27};
Student std2;
std2.rollNumber= 12321031;
std2.Name = "Akshay";
std2.age = 21;
Student std3;
cin>>std3.rollNumber>>std3.Name>>std3.age;
Example:
#include <iostream>
using namespace std;
struct Student{
int rollNumber;
string Name;
int age;
};
int main() {
Student arr[5];
// arr[0].rollNumber = 11231;
// arr[0].Name = "Avi";
// arr[0].age = 21;
//
// arr[1].rollNumber = 23231;
// arr[1].Name = "Savin";
// arr[1].age = 23;
for(int i=0;i<5;i++)
{
cin>>arr[i].rollNumber;
cin>>arr[i].Name;
cin>>arr[i].age;
}
for(int i=0;i<5;i++)
{
cout<<"Information about Student "<<(i+1)<<"\n";
cout<<"Roll Number = "<<arr[i].rollNumber<<"\n";
cout<<"Name = "<<arr[i].Name<<"\n";
cout<<"Age = "<<arr[i].age<<"\n";
cout<<"-------------------\n";
}
return 0;
}
Example:
#include <iostream>
using namespace std;
struct Point{
int x;
int y;
};
void displayPoint(Point p)
{
cout<<"Coordinates = "<<p.x<<","<<p.y<<"\n";
}
int main() {
Point p1 = {3,5};
displayPoint(p1);
Point p2 = modifyPoint(p1,6,9);
displayPoint(p1);
displayPoint(p2);
return 0;
}
Example:
#include <iostream>
using namespace std;
struct Point{
int x;
int y;
};
void displayPoint(Point *p)
{
cout<<"Coordinates = "<<p->x<<","<<p->y<<"\n";
}
int main() {
Point p1 = {3,5};
Point *ptr = &p1;
displayPoint(&p1);
Point* p2 = modifyPoint(ptr, 10,20);
cout<<"After modification\n";
displayPoint(&p1);
displayPoint(p2);
return 0;
}
Example:
#include <iostream>
using namespace std;
struct Point{
int x;
int y;
};
int main() {
Point* p1 = new Point;
p1->x = 4;
p1->y = 8;
displayPoint(p1);
// Point *ptr = &p1;
// displayPoint(&p1);
// Point* p2 = modifyPoint(ptr, 10,20);
// cout<<"After modification\n";
// displayPoint(&p1);
// displayPoint(p2);
return 0;
}