Skip to content

Pointer to Structure

Pointer to Structure

In C and C++, structures are used to group different types of variables under a single name. Pointers to structures allow for more efficient manipulation and access to these grouped variables, particularly in cases where passing entire structures by value would be inefficient.

Definition of Structure

A structure (struct) is a user-defined data type that groups related variables of different data types. Here’s a simple example of a structure definition and how to create a pointer to it.

Declaration and Initialization

Structure Definition

#include <iostream>

using namespace std;

struct Person {

    string name;

    int age;

    float height;

};

int main() {

    Person p1 = {“Alice”, 30, 5.7}; // Create a structure variable

    Person *ptr = &p1; // Pointer to the structure

    // Accessing structure members using the pointer

    cout << “Name: ” << ptr->name << endl;

    cout << “Age: ” << ptr->age << endl;

    cout << “Height: ” << ptr->height << endl;

    return 0;

}

Accessing Members

To access members of a structure using a pointer, the arrow operator (->) is used. This operator dereferences the pointer and accesses the member in a single step.

Example of Pointer to Structure

#include <iostream>

using namespace std;

struct Person {

    string name;

    int age;

    float height;

};

int main() {

    // Declare and initialize a structure variable

    Person p1 = {“Alice”, 30, 5.7};

    // Declare a pointer to the structure

    Person *ptr = &p1;

    // Access and modify structure members using the pointer

    cout << “Initial values:” << endl;

    cout << “Name: ” << ptr->name << endl;

    cout << “Age: ” << ptr->age << endl;

    cout << “Height: ” << ptr->height << endl;

    // Modify the structure members using the pointer

    ptr->name = “Bob”;

    ptr->age = 25;

    ptr->height = 6.0;

    cout << “Modified values:” << endl;

    cout << “Name: ” << ptr->name << endl;

    cout << “Age: ” << ptr->age << endl;

    cout << “Height: ” << ptr->height << endl;

    return 0;

}

Output:

Initial values:

Name: Alice

Age: 30

Height: 5.7

Modified values:

Name: Bob

Age: 25

Height: 6.0

Dynamic Memory Allocation for Structures

Pointers to structures are particularly useful when dealing with dynamic memory allocation. Using new (in C++) or malloc (in C), you can allocate memory for a structure at runtime.

Using new in C++

#include <iostream>

using namespace std;

struct Person {

    string name;

    int age;

    float height;

};

int main() {

    // Allocate memory for a structure dynamically

    Person *ptr = new Person;

    // Access and modify structure members using the pointer

    ptr->name = “Charlie”;

    ptr->age = 22;

    ptr->height = 5.9;

    cout << “Name: ” << ptr->name << endl;

    cout << “Age: ” << ptr->age << endl;

    cout << “Height: ” << ptr->height << endl;

    // Deallocate memory

    delete ptr;

    return 0;

}

Output:

Name: Charlie

Age: 22

Height: 5.9

Using malloc in C

#include <stdio.h>

#include <stdlib.h>

struct Person {

    char name[50];

    int age;

    float height;

};

int main() {

    // Allocate memory for a structure dynamically

    struct Person *ptr = (struct Person*) malloc(sizeof(struct Person));

    // Access and modify structure members using the pointer

    strcpy(ptr->name, “Daisy”);

    ptr->age = 28;

    ptr->height = 5.5;

    printf(“Name: %s\n”, ptr->name);

    printf(“Age: %d\n”, ptr->age);

    printf(“Height: %.1f\n”, ptr->height);

    // Deallocate memory

    free(ptr);

    return 0;

}

Output:

Name: Daisy

Age: 28

Height: 5.5

Arrays of Structures

Pointers to structures can also be used to manage arrays of structures.

Example

#include <iostream>

using namespace std;

struct Person {

    string name;

    int age;

    float height;

};

int main() {

    // Declare and initialize an array of structures

    Person people[3] = {

        {“Alice”, 30, 5.7},

        {“Bob”, 25, 6.0},

        {“Charlie”, 22, 5.9}

    };

    // Declare a pointer to the array of structures

    Person *ptr = people;

    // Access and modify structure members using the pointer

    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;

    }

    return 0;

}

Output:

Name: Alice

Age: 30

Height: 5.7

Name: Bob

Age: 25

Height: 6.0

Name: Charlie

Age: 22

Height: 5.9

Summary

Pointers to structures in C and C++ are a powerful tool for efficient data manipulation and memory management. They allow for indirect access to structure members, dynamic memory allocation, and easy handling of arrays of structures. Understanding how to use pointers to structures effectively is crucial for writing efficient and flexible code in these languages.