C++ Pointers to Structure

In C++, the pointers to a structure variable in very similar to the pointer to any in-built data type variable. As we know that a pointer stores the address of a variable, a pointer to a structure variable stores the base address of structure variable.

Syntax for declaring a pointer to structure variable

struct structure_name *pointer_variable;
For Example :
struct Employee
{
    char name[50];
    int age;
    float salary;
}employee_one;
struct Employee *employee_ptr;

To get the address of a structure variable, we can use addressOf operator(&).

struct Employee *employee_ptr = &employee_one;

To access the member variable using a pointer, we can use arrow operator(->). We can access member variable by using pointer followed by an arrow operator and then the name of the member variable.

employee_ptr->salary;

C++ Pointer to Structure Example Program

#include <iostream>
using namespace std;
 
struct employee {
 char name[100];
 int age;
 float salary;
 char department[50];
};
 
int main(){
   struct employee manager, *ptr;
    
   printf("Enter Name, Age, Salary and Department of Employee\n");
   // Assigning data to members of structure variable
   cin >> manager.name >> manager.age >> manager.salary >> manager.department;
 
        
   /* Printing structure members using arrow operator */
   ptr = &manager;
   cout << "\nEmployee Details\n";
   cout << "Name : " << ptr->name << "\nAge : "<< ptr->age << "\nSalary : "
        << ptr->salary << "\nDepartment : "<< ptr->department;
 
   return 0;
}

Output
Enter Name, Age, Salary and Department of Employee
Dan 42 8453 Accounting

Employee Details
Name : Aman
Age : 42
Salary : 8453
Department : Accounting