-
Hajipur, Bihar, 844101
In C++, a structure (or struct) is a user-defined data type that allows you to group different types of variables under a single name. Unlike arrays, which hold multiple elements of the same type, structures can hold a mix of data types.
Structures are especially useful when dealing with complex entities like students, employees, or products — where multiple properties describe one object.
A structure is a composite data type that groups variables of different data types into a single unit.
Each variable inside a structure is called a member.
struct Student {
int rollNo;
string name;
float marks;
};
Here:
Student is the structure name.
It has three members — an integer rollNo, a string name, and a float marks.
The keyword struct is used to declare a structure.
The general syntax looks like this:
struct structureName {
dataType member1;
dataType member2;
...
};
#include <iostream>
using namespace std;
struct Student {
int rollNo;
string name;
float marks;
};
int main() {
Student s1;
s1.rollNo = 101;
s1.name = "Ananya";
s1.marks = 89.5;
cout << "Roll No: " << s1.rollNo << endl;
cout << "Name: " << s1.name << endl;
cout << "Marks: " << s1.marks;
return 0;
}
Output:
Roll No: 101
Name: Ananya
Marks: 89.5
In this example, we created a structure named Student, declared an object s1, and assigned values to its members.
You can declare structure variables in two ways:
struct Student {
int rollNo;
string name;
float marks;
};
Student s1, s2;
struct Student {
int rollNo;
string name;
float marks;
} s1, s2;
Both methods are valid and depend on how you want to organize your code.
The dot operator (.) is used to access structure members.
Example:
s1.rollNo = 201;
cout << s1.rollNo;
The dot operator tells the compiler which member of the structure is being used.
#include <iostream>
using namespace std;
struct Student {
int rollNo;
string name;
float marks;
};
int main() {
Student s1;
cout << "Enter Roll No: ";
cin >> s1.rollNo;
cout << "Enter Name: ";
cin >> s1.name;
cout << "Enter Marks: ";
cin >> s1.marks;
cout << "\nStudent Details:\n";
cout << "Roll No: " << s1.rollNo << endl;
cout << "Name: " << s1.name << endl;
cout << "Marks: " << s1.marks;
return 0;
}
Output:
Enter Roll No: 5
Enter Name: Meera
Enter Marks: 91.5
Student Details:
Roll No: 5
Name: Meera
Marks: 91.5
You can create an array of structures to store details of multiple entities of the same type.
Example:
#include <iostream>
using namespace std;
struct Student {
int rollNo;
string name;
float marks;
};
int main() {
Student s[3];
for (int i = 0; i < 3; i++) {
cout << "Enter details of student " << i + 1 << ":\n";
cin >> s[i].rollNo >> s[i].name >> s[i].marks;
}
cout << "\nDisplaying Student Information:\n";
for (int i = 0; i < 3; i++) {
cout << s[i].rollNo << " " << s[i].name << " " << s[i].marks << endl;
}
return 0;
}
Output:
Enter details of student 1:
101 Riya 89.5
Enter details of student 2:
102 Nisha 92
Enter details of student 3:
103 Kritika 88
Displaying Student Information:
101 Riya 89.5
102 Nisha 92
103 Kritika 88
A structure can also contain another structure as a member.
This is known as a nested structure.
Example:
#include <iostream>
using namespace std;
struct Address {
string city;
int pincode;
};
struct Student {
string name;
int rollNo;
Address addr;
};
int main() {
Student s1;
s1.name = "Tanya";
s1.rollNo = 101;
s1.addr.city = "Delhi";
s1.addr.pincode = 110001;
cout << "Name: " << s1.name << endl;
cout << "Roll No: " << s1.rollNo << endl;
cout << "City: " << s1.addr.city << endl;
cout << "Pincode: " << s1.addr.pincode;
return 0;
}
Output:
Name: Tanya
Roll No: 101
City: Delhi
Pincode: 110001
Nested structures help in organizing related data more efficiently.
You can pass structures to functions by value or by reference.
#include <iostream>
using namespace std;
struct Student {
string name;
int age;
};
void display(Student s) {
cout << "Name: " << s.name << ", Age: " << s.age;
}
int main() {
Student s1 = {"Priya", 20};
display(s1);
return 0;
}
Output:
Name: Priya, Age: 20
Here, a copy of the structure is passed to the function.
You can use pointers to access structure members dynamically.
Example:
#include <iostream>
using namespace std;
struct Student {
string name;
int age;
};
int main() {
Student s1 = {"Ankita", 19};
Student* ptr = &s1;
cout << "Name: " << ptr->name << endl;
cout << "Age: " << ptr->age;
return 0;
}
Output:
Name: Ankita
Age: 19
Here, the arrow operator (->) is used with pointers to access structure members.
| Feature | Structure | Array |
|---|---|---|
| Data Type | Can hold multiple types | Holds only one data type |
| Access | Members accessed by name | Elements accessed by index |
| Flexibility | Can mix different data | Only homogeneous data |
| Example | Student details | Marks list |
A structure in C++ allows grouping variables of different data types into one unit.
They are the foundation for more advanced concepts like classes, objects, and data abstraction.
Key points to remember:
Use the struct keyword to define a structure.
Access members with the dot operator or arrow operator when using pointers.
You can create arrays, nested structures, and functions that work with structures.
Structures make programs more organized and are essential for handling real-world data efficiently.
Write a program to create a structure named Book with members title, author, and price. Accept and display book details.
Create a structure called Employee with members id, name, and salary. Write a program to input and display details of 5 employees using an array of structures.
Define a structure Car with members brand, model, and price. Accept details for 3 cars and display the one with the highest price.
Write a program to create a structure Student that stores rollNo, name, and marks. Calculate the average marks of 3 students.
Create a nested structure where Student contains another structure Address with members city and pincode. Input and display complete student details.
Write a program to pass a structure Person to a function and display the person’s details using call by value.
Create a structure Product and use a pointer to access and print its members using the arrow operator (->).
Define a structure Movie with members title, rating, and duration. Store information for 4 movies and display the movie with the best rating.
Write a program that uses a structure Rectangle to calculate and display its area and perimeter.
Define a structure Laptop with members brand, processor, and price. Store data for 3 laptops and find the one with the lowest price using a loop.