Pointers to structures in C++
In C++, pointers to structures are a powerful feature that allows you to dynamically access and manipulate the members of a structure. Instead of working directly with the structure variable, you can use a pointer to refer to the structure and its members. This is particularly useful when dealing with dynamic memory allocation or when passing large structures to functions efficiently.
In this guide, we’ll learn how to define and use pointers to structures in C++, including how to access structure members using the pointer and the arrow operator (->
).
A pointer to a structure is simply a pointer that holds the memory address of a structure variable. Instead of accessing structure members directly through the structure variable, you access them through the pointer.
struct StructureName {
data_type member1;
data_type member2;
// Other members
};
StructureName* pointerName; // Pointer to structure
StructureName
: The name of the structure.pointerName
: A pointer that will hold the address of the structure variable.To access members of a structure using a pointer, you use the arrow operator (->
). The arrow operator combines dereferencing the pointer and accessing the member in a single step.
pointerName->memberName;
This is equivalent to:
(*pointerName).memberName;
But using the arrow operator is more concise and easier to read.
Let’s go through an example to understand how to work with pointers to structures.
#include <iostream>
using namespace std;
// Define a structure
struct Person {
string name;
int age;
float height;
};
int main() {
// Create a structure variable
Person person1 = {"Alice", 30, 5.6};
// Create a pointer to the structure
Person* ptr = &person1; // Store the address of person1 in ptr
// Access members of the structure using the pointer
cout << "Name: " << ptr->name << endl;
cout << "Age: " << ptr->age << endl;
cout << "Height: " << ptr->height << endl;
return 0;
}
Output:
Name: Alice
Age: 30
Height: 5.6
Explanation:
Person
structure with three members: name
, age
, and height
.ptr
is a pointer to the Person
structure.->
operator to access the members of person1
via the pointer ptr
.Pointers to structures are often used in dynamic memory allocation. The new
operator allows you to dynamically allocate memory for a structure, and the delete
operator is used to free the allocated memory.
#include <iostream>
using namespace std;
struct Person {
string name;
int age;
float height;
};
int main() {
// Dynamically allocate memory for a Person structure
Person* ptr = new Person;
// Assign values to the members using the pointer
ptr->name = "Bob";
ptr->age = 25;
ptr->height = 5.9;
// Access and print the members using the pointer
cout << "Name: " << ptr->name << endl;
cout << "Age: " << ptr->age << endl;
cout << "Height: " << ptr->height << endl;
// Free the dynamically allocated memory
delete ptr;
return 0;
}
Output:
Name: Bob
Age: 25
Height: 5.9
Explanation:
new Person
to allocate memory for a Person
structure on the heap.ptr
holds the address of this dynamically allocated memory.delete
.Passing a pointer to a structure to a function is a common practice. This allows you to manipulate the structure inside the function without making a copy of it. Using pointers also helps avoid performance issues when dealing with large structures.
#include <iostream>
using namespace std;
struct Person {
string name;
int age;
float height;
};
// Function that accepts a pointer to a structure
void displayPersonInfo(Person* p) {
// Access structure members using the pointer
cout << "Name: " << p->name << endl;
cout << "Age: " << p->age << endl;
cout << "Height: " << p->height << endl;
}
int main() {
// Create a structure variable
Person person1 = {"Charlie", 40, 5.8};
// Pass the pointer to the structure to the function
displayPersonInfo(&person1);
return 0;
}
Output:
Name: Charlie
Age: 40
Height: 5.8
Explanation:
person1
to the displayPersonInfo
function using the address-of operator (&
).->
operator to access the members of the structure.Pointers can also be used to access elements in an array of structures. This is similar to how we use pointers with single structures, but we’ll use pointer arithmetic to move through the array.
#include <iostream>
using namespace std;
struct Person {
string name;
int age;
float height;
};
int main() {
// Array of structure variables
Person people[3] = {
{"Alice", 30, 5.6},
{"Bob", 25, 5.9},
{"Charlie", 40, 5.8}
};
// Pointer to the array
Person* ptr = people;
// Access members using pointer arithmetic
for (int i = 0; i < 3; i++) {
cout << "Name: " << (ptr + i)->name << endl;
cout << "Age: " << (ptr + i)->age << endl;
cout << "Height: " << (ptr + i)->height << endl;
cout << endl;
}
return 0;
}
Output:
Name: Alice
Age: 30
Height: 5.6
Name: Bob
Age: 25
Height: 5.9
Name: Charlie
Age: 40
Height: 5.8
Explanation:
people
of 3 Person
structures.ptr
is a pointer to the first element of the people
array.(ptr + i)
to access each structure in the array and print its members.