Pointers to structures in C++


Introduction

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 (->).


1. What is a Pointer to a Structure?

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.

Syntax for Defining a Pointer to a Structure:

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.

2. Accessing Structure Members Using Pointers

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.

Syntax for Accessing Structure Members:

pointerName->memberName;

This is equivalent to:

(*pointerName).memberName;

But using the arrow operator is more concise and easier to read.


3. Example: Pointer to a Structure

Let’s go through an example to understand how to work with pointers to structures.

Example 1: Defining and Using a Pointer to a Structure

#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:

  • We defined a Person structure with three members: name, age, and height.
  • ptr is a pointer to the Person structure.
  • We used the -> operator to access the members of person1 via the pointer ptr.

4. Dynamically Allocating Memory for a Structure Using Pointers

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.

Example 2: Dynamic Memory Allocation for a Structure

#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:

  • We use new Person to allocate memory for a Person structure on the heap.
  • The pointer ptr holds the address of this dynamically allocated memory.
  • After using the structure, we free the memory using delete.

5. Passing a Pointer to a Structure to a Function

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.

Example 3: Passing a Pointer to a Structure to a Function

#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:

  • We pass the address of person1 to the displayPersonInfo function using the address-of operator (&).
  • Inside the function, we use the -> operator to access the members of the structure.

6. Accessing Array of Structures Using Pointers

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.

Example 4: Array of Structures with Pointers

#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:

  • We have an array people of 3 Person structures.
  • ptr is a pointer to the first element of the people array.
  • We use pointer arithmetic (ptr + i) to access each structure in the array and print its members.