A structure is a user-defined data type that groups related variables of the same or different data types under a single name. It helps organize data and represent real-world entities.
- Structures are declared using the struct keyword.
- They can contain data members, member functions, and constructors.
- Structure members are public by default unless another access level is specified.
- Structures are used to create linked lists, trees, and represent real-world entities like students and faculty.
#include <iostream>
using namespace std;
// Define a structure
struct Point{
int x, y;
};
int main(){
// Create and initialize structure variable
Point p = {10, 20};
// Access members
cout << "x = " << p.x << endl;
cout << "y = " << p.y;
return 0;
}
Output
x = 10 y = 20
Explanation: The Point structure groups two integer members, x and y. The object p stores their values, which are accessed using the dot (.) operator.
Syntax:
A structure is declared using the struct keyword, followed by its name and member declarations.
struct structure_name {
data_type member1;
data_type member2;
};
Here:
- structure_name specifies the name of the structure.
- data_type represents the type of each member.
- member1 and member2 represent the data members.
A structure definition introduces a type. Memory for its non-static data members is allocated when an object of that type is created.

Creating and Initializing Structure Variables
Once a structure is defined, variables can be created using its name without repeating the struct keyword.
Syntax:
structure_name variable_name;
Structure members can be initialized using braces {}. Since C++11, default member initializers are also supported.
#include <iostream>
using namespace std;
struct Student {
int rollNo;
// Default member initializer
int marks = 0;
};
int main(){
// Initialize structure members
Student s1 = {101, 85};
// Use default value for marks
Student s2 = {102};
cout << s1.rollNo << " " << s1.marks << endl;
cout << s2.rollNo << " " << s2.marks;
return 0;
}
Output
101 85 102 0
Explanation: The first object initializes both members, while the second uses the default value 0 for marks.
Note: Since C++20, designated initializers such as Student s = {.rollNo = 101, .marks = 85}; can also be used with aggregate structures. The designators must follow the declaration order.
Accessing and Modifying Structure Members
Structure members are accessed using the dot (.) operator. Their values can be updated using the assignment operator when the members are accessible and modifiable.
Syntax:
variable_name.member_name;
variable_name.member_name = new_value;
#include <iostream>
using namespace std;
struct Point{
int x, y;
};
int main(){
Point p = {10, 20};
// Access a member
cout << "Before: " << p.x << endl;
// Update a member
p.x = 50;
cout << "After: " << p.x;
return 0;
}
Output
Before: 10 After: 50
Explanation: The program accesses p.x and changes its value from 10 to 50.
Member Functions in Structures
C++ structures can contain member functions that operate on their data members. They also support constructors, destructors, and access specifiers.
#include <iostream>
using namespace std;
struct Point {
int x, y;
// Member function
int sum() const {
return x + y;
}
};
int main() {
Point p = {2, 3};
// Call member function
cout << p.sum();
return 0;
}
Output
5
Explanation: The sum() member function returns the sum of x and y.
Size of Structure
The size of a structure depends on its data members and alignment requirements. The compiler may insert padding between or after members.
#include <iostream>
using namespace std;
struct GfG {
char c;
int x, y;
};
int main() {
// Find structure size
cout << "Structure size: "
<< sizeof(GfG) << " bytes";
return 0;
}
Output
Structure size: 12 bytes
Explanation: The structure contains one char and two int members. Padding may increase its size to satisfy alignment requirements.
Note: The result of
sizeof(GfG)is implementation-dependent and may differ across systems.
Nested Structures
A nested structure is a structure declared inside another structure. It helps organize related data within a larger type.
#include <iostream>
using namespace std;
struct Student{
// Nested structure
struct Address {
int houseNo;
int pinCode;
};
int rollNo;
Address address;
};
int main(){
Student s = {101, {25, 110001}};
cout << "Roll No: " << s.rollNo << endl;
cout << "House No: " << s.address.houseNo << endl;
cout << "PIN Code: " << s.address.pinCode;
return 0;
}
Output
Roll No: 101 House No: 25 PIN Code: 110001
Explanation: The Address structure is declared inside Student. Its members are accessed through the address object using the dot operator.
Pointer to Structure
A pointer to a structure stores the address of a structure object. Members can be accessed through the pointer using the arrow (->) operator.
Syntax:
structure_name* pointer = &object;
pointer->member_name;
#include <iostream>
using namespace std;
struct Student{
int rollNo;
int marks;
};
int main(){
Student s = {101, 90};
// Pointer to structure
Student* ptr = &s;
// Access members using arrow operator
cout << "Roll No: " << ptr->rollNo << endl;
cout << "Marks: " << ptr->marks;
return 0;
}
Output
Roll No: 101 Marks: 90
Explanation: The pointer ptr stores the address of s. The -> operator accesses its members and is equivalent to using (*ptr).member_name.
Self-Referential Structures
Self-referential structures are those structures that contains the pointer to the same type as a member. For example,
Such kind of structures are used in data structures such as linked list, trees, etc.
// Self referential structures
struct GfG {
int val;
// Pointer to same type
GfG *next;
};
Note: A structure can only contain pointer to the same type but not variable of same type. It is because it is impossible to derive the size information in the structure before complete definition.
Passing Structures to Functions
Structures can be passed to functions by value, reference, or pointer. Passing large structures by const reference can avoid unnecessary copying when modification is not required.
#include <iostream>
using namespace std;
struct Student{
int rollNo;
int marks;
};
// Pass structure by const reference
void display(const Student& s){
cout << "Roll No: " << s.rollNo << endl;
cout << "Marks: " << s.marks;
}
int main(){
Student s = {101, 95};
display(s);
return 0;
}
Output
Roll No: 101 Marks: 95
Explanation: The function receives s through a const reference, avoiding a copy and preventing modification through that parameter.
Bit Fields in Structures
Bit fields allow integral or enumeration members to be declared with a specified bit width. They are useful when representing compact flags and certain hardware-related data.
Syntax:
struct structure_name {
unsigned int member_name : width;
};
Example:
struct Status {
unsigned int isActive : 1;
unsigned int isVerified : 1;
};
Each field is declared with a width of one bit. However, the overall structure size and bit-field layout depend on the compiler and platform.

