Structures in C++
Written and reviewed by Gagan Bhardwaj · Senior IT Faculty · 15+ years’ experience
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
struct Student {
string name;
int roll;
double marks;
}; // note the semicolonThis 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:
#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;
}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:
Student list[2] = {{"Amit", 1, 90}, {"Riya", 2, 95}};
for (int i = 0; i < 2; i++)
cout << list[i].name << " " << list[i].marks << "\n";Amit 90
Riya 95
Structs with functions
Pass a struct to a function — by const reference to avoid copying large ones:
void printStudent(const Student &s) {
cout << s.name << ": " << s.marks << "\n";
}struct vs class
| struct | class | |
|---|---|---|
| Default access | public | private |
| Typical use | Plain data | Data + behaviour |
| Can have methods? | Yes | Yes |
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
constreference is better. - Expecting struct members to be private — they are public by default.
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
constreference 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++?
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++?
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++?
Can a struct have functions in C++?
How do you make an array of structures in C++?
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.Structure क्या है?
Structure संबंधित data के टुकड़ों को — संभवतः अलग types के — एक नाम के तहत समूहित करता है। एक student के नाम, roll number और marks के लिए अलग variables के बजाय, आप उन्हें एक Student value में बाँध देते हैं।
struct परिभाषित करना
struct Student {
string name;
int roll;
double marks;
}; // semicolon dhyan deinयह Student नामक एक नया type परिभाषित करता है। Members इसके अंदर के variables हैं।
बनाना और access करना
struct type का variable बनाएँ और dot operator से इसके members तक पहुँचें:
#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;
}Amit (12): 88.5
आप एक साथ भी initialise कर सकते हैं: Student s = {"Amit", 12, 88.5};। Pointer के साथ, arrow इस्तेमाल करें: p->name।
Structs का array
कई records रखना आसान है — struct का array बनाएँ:
Student list[2] = {{"Amit", 1, 90}, {"Riya", 2, 95}};
for (int i = 0; i < 2; i++)
cout << list[i].name << " " << list[i].marks << "\n";Amit 90
Riya 95
Functions के साथ structs
Struct को function को भेजें — बड़े को copy से बचाने को const reference द्वारा:
void printStudent(const Student &s) {
cout << s.name << ": " << s.marks << "\n";
}struct बनाम class
| struct | class | |
|---|---|---|
| Default access | public | private |
| सामान्य इस्तेमाल | सादा data | Data + व्यवहार |
| Methods हो सकते हैं? | हाँ | हाँ |
एकमात्र भाषा अंतर default access है। व्यवहार और encapsulation के साथ आगे बढ़ने को classes और objects देखें।
आम गलतियाँ
- struct के closing brace के बाद semicolon भूलना।
- Pointer पर
->के बजाय.इस्तेमाल करना। - बड़े structs को value से copy करना जब
constreference बेहतर है। - struct members को private मानना — वे default रूप से public हैं।
title, author और price वाला एक Book struct परिभाषित करें। तीन books का array बनाएँ, उन्हें पढ़ें, और सबसे महँगी print करें।
सारांश
- struct अलग types के संबंधित data को एक नाम के तहत समूहित करता है।
- Members को dot operator से access करें (या pointer के ज़रिए
->)। - Structs के arrays एक ही आकार के कई records रखते हैं।
- Copies से बचने को structs को functions को
constreference द्वारा भेजें। - struct और class केवल default access में भिन्न हैं (public बनाम private)।