C++

C++ Pointer to Structure

C++ provides a wide range of features for developing applications. One such feature is pointers, which can store memory addresses of variables. Pointers can be particularly useful when working with structures in C++, as they allow for easy manipulation of the structure’s data. This article covers the method to use pointers to structures in C++.

Introduction to Structures in C++

A structure is a data type created by the user which combines multiple variables of different data types into a single entity identified by a single name. The structure is like a container that holds various data types, like integers, floats, and characters, in a single unit.

Introduction to Pointers in C++

A pointer in C++ is a variable that stores the memory address of another variable. They are useful for manipulating and accessing data in memory and are commonly used in dynamic memory allocation and passing parameters to functions by reference.

Declaring a Pointer to Structure in C++

Before we can use a structure and pointers, we have to declare them first. The “struct” keyword is used for the declaration of structure in C++, and it is followed by the name that identifies the structure.

Following is the syntax of the pointer to structure in C++.

struct name_of_structure *ptr;

ptr = &structure_variable;

Syntax declares a pointer variable ptr of type struct name_of_structure. The (*) indicates that ptr is a pointer.

The second line assigns the memory address of a structure variable of type name_of_structure to the pointer ptr using the & (address-of) operator.

How to Create a Pointer to Structure in C++

To create a pointer to structure in C++ following are the steps:

Step 1: First declare a structure with the desired members.

struct Employee{

 string name;

 int age;

 float salary;

};

Step 2: Now we will declare a pointer variable having the same type as the structure. Use the (*) operator to indicate that it’s a pointer variable.

Employee *EmployeePtr;

Step 3: Next use the address-of operator (&) to assign the pointer to the memory address of a structure variable.

Employee p = {"Employee", 24, 10000};

EmployeePtr = &p;

To access the member of the structure we will use the arrow operator (->) with a pointer variable.

cout << "Name: " << EmployeePtr->name << endl;

cout << "Age: " << EmployeePtr->age << endl;

cout << "Salary: " << EmployeePtr->salary << endl;

Below is the complete program that explains how to create a pointer to structure in C++:

#include <iostream>

using namespace std;

struct Employee{

  string name;

  int age;

  float salary;

};

int main() {

  Employee p = {"Employee", 24, 10000};

  Employee *EmployeePtr = &p;

cout << "Name: " << EmployeePtr->name << endl;

cout << "Age: " << EmployeePtr->age << endl;

cout << "Salary: " << EmployeePtr->salary << endl;

  return 0;

}

The above program creates a structured Employee with three members; name, age, and salary. It then creates a variable p of type Employee and initializes its members.

Next, it creates a pointer variable EmployeePtr of type Employee and assigns it the memory address of p. Next it uses the pointer to access the members of the Employee struct and prints them to the console.

Conclusion

Pointers to structures in C++ allow for the manipulation of complex data types with ease. By using pointers to structures, you can access and modify the data contained within a structure and pass it as an argument to functions. For details on C++ pointers to structure, refer to the documentation.

About the author

Kashif

I am an Electrical Engineer. I love to write about electronics. I am passionate about writing and sharing new ideas related to emerging technologies in the field of electronics.