Structures and Unions in C++
Structures and unions allow grouping variables of different data types under a single name. They are the foundation of custom data types in C++ and an important stepping stone toward understanding classes and objects.
Structures — struct
A structure bundles multiple variables (called members) of different types into one unit. Think of it as a record card — a student record can have a name (string), age (int), and marks (float) all in one place.
Defining a Structure:
struct Student {
string name;
int age;
float marks;
};
Creating a Structure Variable:
Student s1;
s1.name = "Arun";
s1.age = 19;
s1.marks = 88.5;
Full Example:
#include <iostream>
#include <string>
using namespace std;
struct Student {
string name;
int age;
float marks;
};
int main() {
Student s1;
s1.name = "Priya";
s1.age = 20;
s1.marks = 92.5;
cout << "Name: " << s1.name << endl;
cout << "Age: " << s1.age << endl;
cout << "Marks: " << s1.marks << endl;
return 0;
}
Output:
Name: Priya
Age: 20
Marks: 92.5Initializing a Struct at Declaration
Student s2 = {"Rohan", 21, 78.0};
Array of Structures
Student batch[3] = {
{"Alice", 19, 85.0},
{"Bob", 20, 78.5},
{"Carol", 19, 91.0}
};
for (int i = 0; i < 3; i++) {
cout << batch[i].name << " - " << batch[i].marks << endl;
}
Output:
Alice - 85
Bob - 78.5
Carol - 91Passing Structures to Functions
void printStudent(Student s) {
cout << s.name << " (Age: " << s.age << ")" << endl;
}
int main() {
Student s = {"Kiran", 22, 80.0};
printStudent(s);
return 0;
}
Output:
Kiran (Age: 22)Nested Structures
A structure can contain another structure as a member:
struct Address {
string city;
string state;
};
struct Person {
string name;
int age;
Address addr; // nested structure
};
int main() {
Person p;
p.name = "Meera";
p.age = 25;
p.addr.city = "Pune";
p.addr.state = "Maharashtra";
cout << p.name << " lives in " << p.addr.city << endl;
return 0;
}
Output:
Meera lives in PuneUnions
A union is similar to a structure, but all members share the same memory location. Only one member can hold a value at a time. The size of a union equals the size of its largest member.
union Data {
int i;
float f;
char c;
};
#include <iostream>
using namespace std;
union Data {
int i;
float f;
};
int main() {
Data d;
d.i = 10;
cout << "int: " << d.i << endl;
d.f = 3.14;
cout << "float: " << d.f << endl;
cout << "int now (corrupted): " << d.i << endl; // i is now overwritten
return 0;
}
Output:
int: 10
float: 3.14
int now (corrupted): 1078523331Struct vs Union
| Feature | struct | union |
|---|---|---|
| Memory | Each member has its own memory | All members share same memory |
| Active members | All members accessible | Only one member at a time |
| Size | Sum of all member sizes | Size of the largest member |
| Use case | Grouping related data | Saving memory, type punning |
Key Takeaways
- A
structgroups multiple variables of different types into one unit. - Members of a struct are accessed using the dot operator
.. - Structures can be nested, placed in arrays, and passed to functions.
- A
unionallows all members to share the same memory — only one member is valid at a time. - Structures are the foundation of OOP concepts like classes in C++.
