Learn how to define and use struct in C++: syntax, initialization, member access, struct vs class, passing by value/reference, and common best practices for safe, efficient structs.
A structure (struct) in C++ is a user-defined data type that allows you to store different types of data under one name.
👉 It is useful when you want to group related data together.
A Student has:
All these can be grouped using a structure.

struct StructureName {
dataType member1;
dataType member2;
};
struct Student {
int rollNo;
string name;
float marks;
};
✔ This only defines the structure, no memory is allocated yet.
After declaring a structure, you can create variables of that structure.
Student s1;
Now s1 can store student data.
Use the dot (.) operator to access members.
#include <iostream>
using namespace std;
struct Student {
int rollNo;
string name;
float marks;
};
int main() {
Student s1;
s1.rollNo = 101;
s1.name = "Ali";
s1.marks = 85.5;
cout << s1.rollNo << endl;
cout << s1.name << endl;
cout << s1.marks << endl;
return 0;
}
Student s1 = {101, "Ahmed", 90.5};
Student s1;
s1.rollNo = 102;
s1.name = "Sara";
s1.marks = 88.0;
In C++, you can directly assign one structure to another of the same type.
Student s1 = {101, "Ali", 85};
Student s2;
s2 = s1; // Copy all values
cout << s2.name; // Output: Ali
✔ All members are copied automatically.
A structure member can also be an array.
struct Student {
int rollNo;
char name[20];
int marks[3];
};
#include <iostream>
using namespace std;
struct Student {
int rollNo;
char name[20];
int marks[3];
};
int main() {
Student s1;
s1.rollNo = 101;
strcpy(s1.name, "Ali");
s1.marks[0] = 80;
s1.marks[1] = 75;
s1.marks[2] = 90;
cout << "Roll No: " << s1.rollNo << endl;
cout << "Name: " << s1.name << endl;
cout << "Marks: ";
for(int i = 0; i < 3; i++) {
cout << s1.marks[i] << " ";
}
return 0;
}
Student students[2] = {
{101, "Ali", 85},
{102, "Sara", 90}
};
✔ Group related data ✔ Improve code readability ✔ Used in real-world data (students, employees, products) ✔ Foundation for classes in OOP
struct groups different data types.)Store and display information of a book using structure.
struct Book {
int bookId;
string title;
float price;
};
#include <iostream>
using namespace std;
struct Book {
int bookId;
string title;
float price;
};
int main() {
// Defining structure variable
Book b1;
// Assigning values
b1.bookId = 1;
b1.title = "C++ Basics";
b1.price = 550.75;
// Displaying values
cout << "Book ID: " << b1.bookId << endl;
cout << "Title: " << b1.title << endl;
cout << "Price: " << b1.price << endl;
return 0;
}
Book ID: 1
Title: C++ Basics
Price: 550.75
Book b2 = {2, "Programming Fundamentals", 620.50};
cout << b2.bookId << endl;
cout << b2.title << endl;
cout << b2.price << endl;
Book b3;
b3 = b2; // Copy all data
cout << b3.title; // Output: Programming Fundamentals
Store and display employee details using a structure.
struct Employee {
int empId;
string name;
float salary;
};
#include <iostream>
using namespace std;
struct Employee {
int empId;
string name;
float salary;
};
int main() {
// Defining structure variable
Employee e1;
// Assigning values
e1.empId = 1001;
e1.name = "Ahmed";
e1.salary = 45000.50;
// Displaying values
cout << "Employee ID: " << e1.empId << endl;
cout << "Employee Name: " << e1.name << endl;
cout << "Employee Salary: " << e1.salary << endl;
return 0;
}
Employee ID: 1001
Employee Name: Ahmed
Employee Salary: 45000.5
Employee e2 = {1002, "Sara", 52000};
Employee e3;
e3 = e1; // Copy data
cout << e3.name; // Output: Ahmed