Introduction
Pointers to structures in C++ allow you to manipulate structure variables through their memory addresses. Using pointers with structures can provide greater flexibility and efficiency, especially when dealing with dynamic memory allocation and passing structures to functions.
Defining and Using Pointers to Structures
Basic Example
You can define a pointer to a structure and use it to access the structure’s members using the arrow operator (->
).
Example: Pointer to a Structure
#include <iostream>
using namespace std;
// Define a structure
struct Person {
string name;
int age;
};
int main() {
// Create a structure variable
Person person = {"Alice", 30};
// Define a pointer to the structure
Person* ptr = &person;
// Access members using the pointer
cout << "Name: " << ptr->name << endl;
cout << "Age: " << ptr->age << endl;
// Modify members using the pointer
ptr->name = "Bob";
ptr->age = 25;
// Print modified members
cout << "Modified Name: " << ptr->name << endl;
cout << "Modified Age: " << ptr->age << endl;
return 0;
}
Output
Name: Alice
Age: 30
Modified Name: Bob
Modified Age: 25
Explanation
Person* ptr = &person;
setsptr
to point to the structure variableperson
.ptr->name
andptr->age
access the members of the structure using the pointer.- The members are modified using the pointer, and the modified values are printed.
Dynamic Memory Allocation for Structures
You can dynamically allocate memory for structures using pointers and the new
operator.
Example: Dynamic Memory Allocation for Structures
#include <iostream>
using namespace std;
// Define a structure
struct Person {
string name;
int age;
};
int main() {
// Dynamically allocate memory for a structure
Person* ptr = new Person;
// Access and modify members using the pointer
ptr->name = "Charlie";
ptr->age = 35;
// Print the members
cout << "Name: " << ptr->name << endl;
cout << "Age: " << ptr->age << endl;
// Deallocate memory
delete ptr;
return 0;
}
Output
Name: Charlie
Age: 35
Explanation
Person* ptr = new Person;
dynamically allocates memory for aPerson
structure.- The members are accessed and modified using the pointer.
delete ptr;
deallocates the memory allocated for the structure.
Array of Structures Using Pointers
You can create an array of structures dynamically using pointers.
Example: Array of Structures
#include <iostream>
using namespace std;
// Define a structure
struct Person {
string name;
int age;
};
int main() {
int numPersons = 3;
// Dynamically allocate memory for an array of structures
Person* persons = new Person[numPersons];
// Access and modify members of the array of structures
for (int i = 0; i < numPersons; i++) {
cout << "Enter name for person " << i + 1 << ": ";
cin >> persons[i].name;
cout << "Enter age for person " << i + 1 << ": ";
cin >> persons[i].age;
}
// Print the members of the array of structures
for (int i = 0; i < numPersons; i++) {
cout << "Person " << i + 1 << ": Name: " << persons[i].name << ", Age: " << persons[i].age << endl;
}
// Deallocate memory
delete[] persons;
return 0;
}
Output
Enter name for person 1: Alice
Enter age for person 1: 30
Enter name for person 2: Bob
Enter age for person 2: 25
Enter name for person 3: Charlie
Enter age for person 3: 35
Person 1: Name: Alice, Age: 30
Person 2: Name: Bob, Age: 25
Person 3: Name: Charlie, Age: 35
Explanation
Person* persons = new Person[numPersons];
dynamically allocates memory for an array ofPerson
structures.- The members of the array of structures are accessed and modified using the pointer.
delete[] persons;
deallocates the memory allocated for the array of structures.
Passing Structures to Functions Using Pointers
You can pass structures to functions using pointers, which allows the function to modify the original structure members.
Example: Passing Structures to Functions
#include <iostream>
using namespace std;
// Define a structure
struct Person {
string name;
int age;
};
// Function to modify the structure members
void modifyPerson(Person* ptr) {
ptr->name = "David";
ptr->age = 40;
}
int main() {
// Create a structure variable
Person person = {"Alice", 30};
// Pass the structure to the function using a pointer
modifyPerson(&person);
// Print the modified members
cout << "Modified Name: " << person.name << endl;
cout << "Modified Age: " << person.age << endl;
return 0;
}
Output
Modified Name: David
Modified Age: 40
Explanation
- The
modifyPerson
function takes a pointer to aPerson
structure as an argument. - The function modifies the structure members using the pointer.
- The modified members are printed in the
main
function.
Example Programs
Example 1: Finding the Oldest Person
#include <iostream>
using namespace std;
// Define a structure
struct Person {
string name;
int age;
};
// Function to find the oldest person
Person* findOldest(Person* persons, int size) {
Person* oldest = &persons[0];
for (int i = 1; i < size; i++) {
if (persons[i].age > oldest->age) {
oldest = &persons[i];
}
}
return oldest;
}
int main() {
int numPersons = 3;
// Dynamically allocate memory for an array of structures
Person* persons = new Person[numPersons];
// Input person details
for (int i = 0; i < numPersons; i++) {
cout << "Enter name for person " << i + 1 << ": ";
cin >> persons[i].name;
cout << "Enter age for person " << i + 1 << ": ";
cin >> persons[i].age;
}
// Find the oldest person
Person* oldest = findOldest(persons, numPersons);
// Print the details of the oldest person
cout << "The oldest person is: Name: " << oldest->name << ", Age: " << oldest->age << endl;
// Deallocate memory
delete[] persons;
return 0;
}
Output
Enter name for person 1: Alice
Enter age for person 1: 30
Enter name for person 2: Bob
Enter age for person 2: 25
Enter name for person 3: Charlie
Enter age for person 3: 35
The oldest person is: Name: Charlie, Age: 35
Explanation
- The
findOldest
function takes a pointer to an array ofPerson
structures and its size as arguments. - The function iterates through the array to find and return a pointer to the oldest person.
- The details of the oldest person are printed in the
main
function.
Example 2: Copying a Structure
#include <iostream>
using namespace std;
// Define a structure
struct Person {
string name;
int age;
};
// Function to copy a structure
void copyPerson(Person* dest, const Person* src) {
dest->name = src->name;
dest->age = src->age;
}
int main() {
// Create a source structure
Person src = {"Alice", 30};
// Create a destination structure
Person dest;
// Copy the source structure to the destination structure
copyPerson(&dest, &src);
// Print the copied members
cout << "Copied Name: " << dest.name << endl;
cout << "Copied Age: " << dest.age << endl;
return 0;
}
Output
Copied Name: Alice
Copied Age: 30
Explanation
- The
copyPerson
function takes pointers to the destination and sourcePerson
structures as arguments. - The function copies the members from the source structure to the destination structure using the pointers.
- The copied members are printed in the
main
function.
Conclusion
Pointers to structures in C++ allow for efficient and flexible manipulation of structure variables through their memory addresses. This chapter covered the basics of defining and using pointers to structures, dynamic memory allocation for structures, creating arrays of structures using pointers, and passing structures to functions using pointers. Example programs demonstrated practical applications of pointers to structures, including finding the oldest person and copying a structure. Understanding how to use pointers to structures effectively is essential for writing efficient and flexible C++ programs.