Sometimes a program needs to store different types of information together.
For example, a student may have a:
- Name
- Age
- Marks
A C++ structure (struct) allows us to group related data under one name.
What is a Structure?
A structure is a user-defined data type that can contain different types of data.
For example:
struct Student {
string name;
int age;
double marks;
};
Here, Student can store a student’s name, age, and marks.
Creating a Structure Object
After creating a structure, you can create an object of that structure.
Student student;
Here, student is a structure object.
Let’s see a complete example:
#include <iostream>
#include <string>
using namespace std;
struct Student {
string name;
int age;
double marks;
};
int main() {
Student student;
student.name = "Rahul";
student.age = 14;
student.marks = 88.5;
cout << "Name: " << student.name << "\n";
cout << "Age: " << student.age << "\n";
cout << "Marks: " << student.marks;
return 0;
}
Output:
Name: Rahul
Age: 14
Marks: 88.5
The dot (.) operator is used to access the members of a structure object.
student.name
student.age
student.marks
Initializing a Structure Object
You can also provide values when creating the object.
Student student = {"Anjali", 15, 92.5};
Complete example:
#include <iostream>
#include <string>
using namespace std;
struct Student {
string name;
int age;
double marks;
};
int main() {
Student student = {"Anjali", 15, 92.5};
cout << "Name: " << student.name << "\n";
cout << "Age: " << student.age << "\n";
cout << "Marks: " << student.marks;
return 0;
}
Output:
Name: Anjali
Age: 15
Marks: 92.5
Array of Structures
You can create an array of structures when you need to store information about multiple objects.
For example, we can store information about three students:
#include <iostream>
#include <string>
using namespace std;
struct Student {
string name;
int age;
};
int main() {
Student students[3] = {
{"Rahul", 14},
{"Anjali", 15},
{"Aman", 14}
};
for (int i = 0; i < 3; i++) {
cout << "Name: " << students[i].name << "\n";
cout << "Age: " << students[i].age << "\n\n";
}
return 0;
}
Output:
Name: Rahul
Age: 14
Name: Anjali
Age: 15
Name: Aman
Age: 14
Here:
Student students[3];
creates an array that can store three Student objects.
You can access each object using its index:
students[0]
students[1]
students[2]
Pointer to a Structure
A pointer can also store the address of a structure object.
For example:
Student* ptr = &student;
Here, ptr stores the address of the student object.
To access structure members through a pointer, use the arrow (->) operator.
ptr->name
ptr->age
Example:
#include <iostream>
#include <string>
using namespace std;
struct Student {
string name;
int age;
};
int main() {
Student student = {"Rahul", 14};
Student* ptr = &student;
cout << "Name: " << ptr->name << "\n";
cout << "Age: " << ptr->age;
return 0;
}
Output:
Name: Rahul
Age: 14
The arrow operator -> is used instead of the dot operator when accessing members through a structure pointer.
Structure with a Function
A structure can also be passed to a function.
#include <iostream>
#include <string>
using namespace std;
struct Student {
string name;
int age;
};
void displayStudent(Student student) {
cout << "Name: " << student.name << "\n";
cout << "Age: " << student.age;
}
int main() {
Student student = {"Rahul", 14};
displayStudent(student);
return 0;
}
Output:
Name: Rahul
Age: 14
A Simple Example
Let’s combine a structure with an array and a loop to display product information.
#include <iostream>
#include <string>
using namespace std;
struct Product {
string name;
double price;
};
int main() {
Product products[3] = {
{"Laptop", 45000},
{"Mouse", 800},
{"Keyboard", 1500}
};
for (int i = 0; i < 3; i++) {
cout << products[i].name << " - ";
cout << products[i].price << "\n";
}
return 0;
}
Output:
Laptop - 45000
Mouse - 800
Keyboard - 1500
Key Points to Remember
- A structure groups different types of data together.
- A structure object is created using the structure name.
- Use the
.operator to access members of a structure object. - An array of structures stores multiple structure objects.
- A pointer can store the address of a structure object.
- Use the
->operator to access members through a structure pointer. - Structures can also be passed to functions.
What’s Next?
In the next chapter, we will learn about C++ Classes and Objects, which are the foundation of object-oriented programming in C++.
Frequently Asked Questions (FAQs)
Q1. What are C++ Structures?
C++ Structures are user-defined data types that group related data of different types under one name.
Q2. How do you create a structure in C++?
Use the struct keyword followed by the structure name and its members, such as struct Student { string name; int age; };.
Q3. What is an Array of Structures in C++?
An Array of Structures in C++ stores multiple objects of the same structure type, making it useful for managing records such as multiple students or products.
Q4. How is a Structure Pointer in C++ used?
A Structure Pointer in C++ stores the address of a structure object and uses the -> operator to access its members.
Q5. What is the difference between . and -> in C++ Structures?
The . operator accesses members through a structure object, while -> accesses members through a pointer to a structure.
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
