Structures in C++: Need and Usage With Examples
Learn how to create custom data types that group related variables together. Master struct definition, member access, arrays of structures, and passing structures to functions.
- What structures are and why they are useful
- How to define and use custom data types
- How to access and modify structure members
- How to use structures in arrays and functions
- How to create nested structures
1Why use structures?
Sometimes, you need to group multiple variables that logically belong together. For example, if you are storing student data:
string name; int roll; float gpa;
Managing these separately becomes messy, especially for many students. A structure lets you group related variables into one unit, making your code cleaner, more organized, and easier to maintain.
2Defining a structure
Syntax:
struct StructureName { type member1; type member2; // ... };
Example – defining a Student structure:
struct Student { string name; int roll; float gpa; };
This defines a new type Student that you can now use like any other data type.
3Declaring structure variables
Once a structure is defined, you can declare variables of that type like any other built-in type.
Student s1; // Declare a Student variable s1.name = "Ali"; s1.roll = 101; s1.gpa = 3.85;
Accessing members: Use the dot . operator.
cout << s1.name << " has GPA " << s1.gpa << endl;
4Structure initialization
C++11 and later allow you to initialize a structure directly using curly braces.
Student s2 = {"Sara", 102, 3.92};
You can also use designated initializers (C++20):
Student s3 = {.name = "Zara", .roll = 103, .gpa = 3.78};
5Array of structures
You can store multiple structures in an array, making it easy to manage collections of related data.
Student students[3]; for (int i = 0; i < 3; i++) { cout << "Enter name, roll, GPA: "; cin >> students[i].name >> students[i].roll >> students[i].gpa; } for (int i = 0; i < 3; i++) { cout << students[i].name << " (" << students[i].roll << ") - " << students[i].gpa << endl; }
6Passing structures to functions
You can pass a structure to a function by value or by reference.
Pass by value
void printStudent(Student s) { cout << s.name << ", Roll: " << s.roll << ", GPA: " << s.gpa << endl; }
Pass by reference
void updateGPA(Student &s, float newGPA) { s.gpa = newGPA; }
&) to avoid copying the entire structure. This is faster and uses less memory.
7Nested structures
You can use one structure inside another. This is called a nested structure.
struct Date { int day, month, year; }; struct Student { string name; int roll; float gpa; Date dob; // date of birth };
Usage:
Student s; s.name = "Zara"; s.dob.day = 15; s.dob.month = 8; s.dob.year = 2004;
8Real-world example: Basic student database
This example demonstrates a simple student database using an array of structures.
#include <iostream> #include <string> using namespace std; struct Student { string name; int roll; float gpa; }; int main() { const int n = 2; Student students[n]; for (int i = 0; i < n; i++) { cout << "Enter name, roll, GPA: "; cin >> students[i].name >> students[i].roll >> students[i].gpa; } cout << "\nStudent List:\n"; for (int i = 0; i < n; i++) { cout << students[i].name << " - " << students[i].roll << " - " << students[i].gpa << endl; } return 0; }
9Common mistakes to avoid
10Mini exercises
- Define a structure
Bookwithtitle,author, andprice. Create an array of books and display them. - Create a structure
Timewithhoursandminutes. Write a function to add two time values. - Use nested structures to store
Employeedata with a nestedAddressstructure containingstreet,city, andzip.
Hint: For adding time, handle minutes that exceed 60 by carrying over to hours.
