Structures in C++ | Need and Usage With Examples

What You’ll Learn

By the end of this post, you will:

  • Understand what structures (struct) are in C++
  • Learn how to define and use custom data types
  • Access and modify structure members
  • Use structures in arrays and functions
  • Apply structures in basic real-world examples

Why Use Structures?

Sometimes, you need to group multiple variables that logically belong together.

Example:

If you’re 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.


Defining a Structure

Syntax:

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

Example:

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

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


Declaring Structure Variables

Example:

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;

Structure Initialization (C++11 and above)

You can initialize a structure directly:

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

Array of Structures

You can store multiple structures in an array:

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;
}

Passing Structures to Functions

You can pass a structure to a function by value or 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;
}

Structures Inside Structures (Nested)

You can use a structure inside another structure.

Example:

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

struct Student {
string name;
Date dob; // date of birth
};

Usage:

Student s;
s.name = "Zara";
s.dob.day = 15;
s.dob.month = 8;
s.dob.year = 2004;

Real-World Example: Basic Student Database

#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;
}

Quiz: Structures

Question 1/7

Mini Exercises

  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.

Common Mistakes to Avoid

  • Forgetting the semicolon after structure definition
  • Accessing structure members before assigning values
  • Passing large structures by value when reference is more efficient

Summary

In this post, you learned:

  • What structures are and why they’re useful
  • How to define, initialize, and use structures
  • How to store multiple structures using arrays
  • How to use structures with functions and nesting

What’s Next?

In the next post, we’ll dive into Operator Overloading in C++ — how classes build on structures to support encapsulation, constructors, and methods.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top