Structs are versatile in C++ programming and can be combined with pointers, arrays, and functions to create flexible and efficient programs.
Structure Pointers
A pointer to a structure allows access to the struct's members indirectly. When accessing struct members through a pointer, the arrow operator (->) is used instead of the dot operator.
#include <iostream>
using namespace std;
struct Point {
int x;
int y;
};
int main() {
Point p = {10, 20};
Point *ptr = &p; // Pointer to the struct
cout << ptr->x << " "; // Access members using the arrow operator
ptr->x = 30;
cout << p.x;
return 0;
}
Output
10 30
Explanation:
- ptr->x accesses x of the struct pointed to by ptr.
- Changing x through the pointer also updates p.x because both refer to the same memory location.
Array of Structures
An array of structures allows creating and managing multiple instances of a struct simultaneously.
#include <iostream>
using namespace std;
struct Point {
int x;
int y;
};
int main() {
Point arr[5];
// Initialize members of each struct in the array
for (int i = 0; i < 5; i++) {
arr[i].x = i;
arr[i].y = i * 10;
}
// Print the members
for (int i = 0; i < 5; i++) {
cout << arr[i].x << " " << arr[i].y << endl;
}
return 0;
}
Output
0 0 1 10 2 20 3 30 4 40
Explanation:
- Each element in the array is a separate struct object.
- Use arr[i].x and arr[i].y to access members of the i-th struct in the array.
Passing Structures as Arguments to Functions
Structs can be passed to functions either by value, by reference, or by pointer.
1. Passing by Value
When a struct is passed by value, a copy is created. This is inefficient for large structs and does not allow modification of the original struct.
#include <iostream>
using namespace std;
struct Student {
int marks;
};
// Passed by value (copy is made)
void update(Student s) {
s.marks = 100; // modifies only the copy
}
int main() {
Student st;
st.marks = 50;
update(st);
cout << "Marks after function call: " << st.marks << endl;
return 0;
}
Explanation:
- update() receives a copy of st.
- Changes affect only the copy, not the original.
- Original value remains unchanged.
2. Passing by Reference
Passing by reference avoids creating a copy and allows the function to modify the original struct.
#include <iostream>
using namespace std;
struct Student {
int roll;
string name;
string address;
};
void print(const Student &s) {
cout << s.roll << " "
<< s.name << " "
<< s.address << endl;
}
int main() {
Student s = {101, "Alice", "Wonderland"};
print(s); // Pass by reference
return 0;
}
Output
101 Alice Wonderland
Advantages:
- Avoids unnecessary copying of large structs.
- Using const ensures the function cannot modify the struct.
3. Passing by Pointer
Passing by pointer is another efficient method. Members of the struct are accessed using the arrow operator (->).
#include <iostream>
using namespace std;
struct Student {
int roll;
string name;
string address;
};
void print(const Student *s) {
cout << s->roll << " "
<< s->name << " "
<< s->address << endl;
}
int main() {
Student s = {101, "Bob", "Narnia"};
print(&s); // Pass by pointer
return 0;
}
Output
101 Bob Narnia
Explanation:
- Similar to passing by reference but explicitly uses pointers.
- Useful when working with dynamic memory.
Avoiding Common Mistakes
- Copy Overhead: Passing structs by value is inefficient for large structs. Always prefer passing by reference or pointer for larger objects.
- Unnecessary Modifications: Use const with references or pointers to prevent accidental modifications of struct members.