Structure in C++

Introduction to structure

  • Structure is a collection of data items of different data types grouped together under one name.
  • It is a user defined data type which stores related information together.
  • Structure is also know as a collection of variables under a single name.
  • The struct keyword is used to define structure.
Syntax:
struct struct_name
{
     member 1;
     member 2;
     .
     .
     member n;
};

Where,
     The struct keyword is required while defining the structure.
     The struct_name is the name given to the structure.
     The semicolon(;) should be used after the closing brace brackets.

Example

struct employee
{
     int empid;
     string empname;
     float salary;
};

Member Access Operator

  • Member Access Operator (.) is used to access any member of structure.
  • This operator is coded as a period between the structure variable name and the structure member.
  • The members of the structure can be accessed on an individual basis.
Syntax:
structure_variable_name.member;

Example : Demonstrating the structure declaration

#include<iostream>
using namespace std;
struct employee
{
    int empid=101;
    string empname="ABC";
    float salary=20000.0;
};
int main()
{
    employee e;       //There is now variable e of structure employee that has modifiable
    cout<<"Employee Id : "<<e.empid<<endl; // variables inside it.
    cout<<"Employee Name : "<<e.empname<<endl;
    cout<<"Employee Salary : "<<e.salary<<endl;
    return 0;
}


Output:
Employee Id : 101
Employee Name : ABC
Employee Salary : 20000

  • In the above example, a structure employee is defined which has three members: empid, empname, salary. Inside main() structure variable of e is defined and '.' operator is used to access the structure variables.
  • When structure is created, no memory is allocated.
  • It is only the blueprint for the creating of variables.
  • When structure is allocated, only required memory is allocated by the compiler.

Pointer to Structure

  • Structure can be pointed to by its own type of pointers.
  • A pointer to a structure can be used by the '&' operator.
  • Declarations of pointers to members are special cases of pointer declarations.

Example : Program demonstrating the pointer to structure

#include<iostream>
using namespace std;
int display(struct Employee * emp);
struct Employee
{
     int empid;
     string empname;
     float salary;
};
int main()
{
     Employee e;
     e.empid=101;
     e.empname="ABC";
     e.salary=20000.0;
     display(&e);
     return 0;
}
int display(struct Employee * emp)
{
     cout<<"Employee Id : "<<emp → empid<<endl;
     cout<<"Employee Name : "<<emp → empname<<endl;
     cout<<"Employee Salary : "<<emp → salary<<endl;
}


Output:
Employee Id : 101
Employee Name : ABC
Employee Salary : 20000

  • In the above program, emp is a structure type pointer variable. The member access '.' operator requires a structure variable whereas the arrow '→' operator requires a structure pointer on its left side.
  • Arrow '→' operator is used to access the members of a structure using pointer to that structure. It is a dereferenced operator which is used exclusively with pointers to objects that have members. This operator is used to access the member of an object directly from its address.