Free tutorials & notes in Hindi & English · Clean code examples 🧰 Free PDF & Image Tools →
🔵 Core C++  ·  Lesson 28

Structures in C++

In this lesson: Group related data with struct in C++ — members, arrays of structs, functions — with output. In Hindi & English.

What is a structure?

A structure groups related pieces of data — of possibly different types — under one name. Instead of separate variables for a student's name, roll number and marks, you bundle them into one Student value.

Defining a struct

C++
struct Student {
    string name;
    int roll;
    double marks;
};   // note the semicolon

This defines a new type called Student. The members are the variables inside it.

Creating and accessing

Create a variable of the struct type and reach its members with the dot operator:

C++
#include <iostream>
using namespace std;
struct Student { string name; int roll; double marks; };

int main() {
    Student s;
    s.name = "Amit";
    s.roll = 12;
    s.marks = 88.5;
    cout << s.name << " (" << s.roll << "): " << s.marks << "\n";
    return 0;
}
Output:
Amit (12): 88.5

You can also initialise at once: Student s = {"Amit", 12, 88.5};. With a pointer, use the arrow: p->name.

Array of structs

Storing many records is easy — make an array of the struct:

C++
Student list[2] = {{"Amit", 1, 90}, {"Riya", 2, 95}};
for (int i = 0; i < 2; i++)
    cout << list[i].name << " " << list[i].marks << "\n";
Dry Run — dekhiye har step kaise chalta hai (i = 0 se shuru)
Stepi ki valueConditionKya huai ab
100 < 2 → trueloop body chalta haii → 1
211 < 2 → trueloop body chalta haii → 2
322 < 2 → falseCondition false — loop yahin ruk gaya (STOP)—
Output:
Amit 90
Riya 95

Structs with functions

Pass a struct to a function — by const reference to avoid copying large ones:

C++
void printStudent(const Student &s) {
    cout << s.name << ": " << s.marks << "\n";
}

struct vs class

structclass
Default accesspublicprivate
Typical usePlain dataData + behaviour
Can have methods?YesYes

The only language difference is default access. See classes and objects to go further with behaviour and encapsulation.

Common mistakes

  • Forgetting the semicolon after the closing brace of a struct.
  • Using . on a pointer instead of ->.
  • Copying large structs by value when a const reference is better.
  • Expecting struct members to be private — they are public by default.
🏋️ Practice

Define a Book struct with title, author and price. Make an array of three books, read them in, and print the most expensive one.

Summary

  • A struct groups related data of different types under one name.
  • Access members with the dot operator (or -> via a pointer).
  • Arrays of structs store many records of the same shape.
  • Pass structs by const reference to functions to avoid copies.
  • struct and class differ only in default access (public vs private).

Frequently Asked Questions

What is a structure in C++?
A structure, declared with struct, groups related variables of different types under one name. For example a Student struct can hold a name, roll number and marks together, so you can treat one student as a single value instead of separate variables.
How do you access members of a struct in C++?
You use the dot operator on a struct variable, such as s.name or s.marks. If you have a pointer to a struct, you use the arrow operator instead, like p->name, which is shorthand for (*p).name.
What is the difference between struct and class in C++?
In C++ a struct and a class are almost identical; the only real difference is default access. Members of a struct are public by default, while members of a class are private by default. By convention, structs are used for plain data and classes for types with behaviour.
Can a struct have functions in C++?
Yes. Unlike C, a C++ struct can contain member functions as well as data, because struct and class are nearly the same. In practice, though, structs are usually kept as simple data holders and classes are used when you need methods and encapsulation.
How do you make an array of structures in C++?
You declare an array whose element type is the struct, such as Student list[3];, and then access each element and its members together, like list[0].name. This is handy for storing many records of the same shape, such as a list of students.
← Back to C++ Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.
Next topicEnumerations in C++ Your progress: 28 / 81 (35%) · Lesson 29 / 81 Continue →

How was this lesson?

Your feedback helps us make the content better.

Write a review or suggestion (optional)

After approval, your name and city may appear publicly with your review. Please do not write your email or phone number.

▶ CodingEasily on YouTube

Learn with video lessons in easy Hindi

Featured video: RDBMS Part 3 – Class XI Commerce IT

▶ Subscribe – it’s free