Structure-in-C++ .

In this session i have discussed structure in C++ .


Structure is a user defined data type in C++ . It is used to stored the various items with different data type .

i.e A person has name , address , salary , age etc . We can stored all in a structure .


Structure syntax

    struct struct_name
    {
        int item1;
        char item2;
        float item3;

    };
    

In above code use the struct keyword for declare the structure . struct_name is the name of structure . In body there are various items with different data types .


Declare the structure

    struct Person
    {
        char name[20];
        int age;
        float salary;
    };
    

Create the struct variable

When a structure is defined, it creates a user-defined type. However, no storage or memory is allocated. To allocate memory of a given structure type and work with it, we need to create variables.

    struct Person
    {
        char name[20];
        int age;
        float salary;
    };
    int main()
    {
        struct Person p1, p2, p3[20];
        return 0;
    }
    

We have declared the p1 , p2 , p3[20] as struct variable .


How to access struct members .

There are two ways by which we can access the struct members .

1-Member operator .

2-Structure pointer operator ->

Suppose if you want to access the age of struct the write below code

p1.age


Example : Write a program demonstrate the structure in C++ .

    #include<iostream>
    using namespace std;   
    struct person
    {
        char name[20];
        int age;
        int salary;
    };
    int main()
    {
        struct person p;
        cout<<"Enter the name :";
        cin>>p.name;
        cout<<"Enter the age";
        cin>>p.age;
        cout<<"Enter the salary";
        cin>>p.salary;
        cout<<"-------------------------------\n";
        cout<<"Name : \n"<<p.name;
        cout<<"Age : \n"<<p.age;
        cout<<"Salary : \n"<<p.salary;
       return (0);
    }
    OUTPUT : Enter the Name : Rakesh
             Enter the Age  : 20
             Enter the Salary : 20000
             -------------------------------------
             Name : Rakesh
             Age : 20
             Salary : 20000
    


Example : Write a program that stored the two records in struct .

    #include<iostream>
    using namespace std;   
    struct person
    {
        char name[20];
        int age;
        int salary;
    };
    int main()
    {
        int i;
        struct person p[2];
        for(i=0;i<2;i++)
        {
            cout<<"Enter the name :";
            cin>>p[i].name;
            cout<<"Enter the age";
            cin>>p[i].age;
            cout<<"Enter the salary";
            cin>>p[i].salary;
        }
        cout<<"-------------------------------\n";
        for(i=0;i<2;i++)
        {
            cout<<"\nName : "<<p[i].name;
            cout<<"\nAge : "<<p[i].age;
            cout<<"\nSalary : "<<p[i].salary;
        }
        return (0);
    }

    
    OUTPUT : Enter the Name : Rakesh
             Enter the Age  : 20
             Enter the Salary : 20000
             Enter the Name : Shashank
             Enter the Age  : 23
             Enter the Salary : 30000
            
             -------------------------------------
             Name : Rakesh
             Age : 20
             Salary : 20000
             Name : Shashank
             Age : 21
             Salary : 30000
    


Welcome to AndroidATPoint.

back image                     next image