C++ Basics · Chapter 11

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.

By Arj Updated July 2025 13 min read No prior experience needed
What you'll learn
  • 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:

cpp
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.

Structures are the foundation of data organization
Structures are one of the most important features in C++. They are the precursor to classes in object-oriented programming and are used extensively in real-world C++ applications.

2Defining a structure

Syntax:

cpp
struct StructureName {
    type member1;
    type member2;
    // ...
};

Example – defining a Student structure:

cpp
struct Student {
    string name;
    int roll;
    float gpa;
};

This defines a new type Student that you can now use like any other data type.

Don't forget the semicolon
The semicolon after the closing brace is required. Forgetting it is a common syntax error.

3Declaring structure variables

Once a structure is defined, you can declare variables of that type like any other built-in type.

cpp
Student s1;  // Declare a Student variable
s1.name = "Ali";
s1.roll = 101;
s1.gpa = 3.85;

Accessing members: Use the dot . operator.

cpp
cout << s1.name << " has GPA " << s1.gpa << endl;
OutputAli has GPA 3.85

4Structure initialization

C++11 and later allow you to initialize a structure directly using curly braces.

cpp
Student s2 = {"Sara", 102, 3.92};

You can also use designated initializers (C++20):

cpp
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.

cpp
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;
}
Sample runEnter name, roll, GPA: Ali 101 3.85 Enter name, roll, GPA: Sara 102 3.92 Enter name, roll, GPA: Zara 103 3.78 Ali (101) - 3.85 Sara (102) - 3.92 Zara (103) - 3.78

6Passing structures to functions

You can pass a structure to a function by value or by reference.

Pass by value

cpp
void printStudent(Student s) {
    cout << s.name << ", Roll: " << s.roll << ", GPA: " << s.gpa << endl;
}

Pass by reference

cpp
void updateGPA(Student &s, float newGPA) {
    s.gpa = newGPA;
}
Pass by reference for efficiency
When passing large structures, pass by reference (&) 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.

cpp
struct Date {
    int day, month, year;
};

struct Student {
    string name;
    int roll;
    float gpa;
    Date dob; // date of birth
};

Usage:

cpp
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.

cpp
#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;
}
Sample runEnter name, roll, GPA: John 201 3.65 Enter name, roll, GPA: Emma 202 3.91 Student List: John - 201 - 3.65 Emma - 202 - 3.91

9Common mistakes to avoid

1. Forgetting the semicolon after the structure definition
This is a common syntax error. The compiler expects a semicolon after the closing brace.
2. Accessing structure members before assigning values
Accessing uninitialized members leads to undefined behavior. Always initialize structure members before using them.
3. Passing large structures by value
Passing large structures by value copies the entire structure, which is inefficient. Use pass by reference instead.
4. Confusing the dot operator (.) with the arrow operator (->)
Use the dot operator for structure variables. The arrow operator is used with pointers to structures.

10Mini exercises

Try these yourself
  1. Define a structure Book with title, author, and price. Create an array of books and display them.
  2. Create a structure Time with hours and minutes. Write a function to add two time values.
  3. Use nested structures to store Employee data with a nested Address structure containing street, city, and zip.

Hint: For adding time, handle minutes that exceed 60 by carrying over to hours.

11Quiz yourself

Structures Quiz
Question 1 of 10
Coming up next
Operator Overloading in C++
Learn how classes build on structures to support encapsulation, constructors, and methods – and how to overload operators for custom types.
Continue to Chapter 12

Stop wrestling with confusion.

Join thousands of students mastering Computer Science without the academic jargon.

From syntax to systems. We break down the hardest ideas in computer science so you can actually build things.

© 2026 Painless Programming. Built for students.
Scroll to Top