C++ Data Structures

    Using an Array Data Structure, we can combine many data values of similar data types, but what if we want a data structure which can hold different values of different data types, to solve this problem in C++ we have another Data Structure known as Structure .

    What is Structure in C++?

    In C++ the structure is a User-Defined Data Structure, which can be used to hold groups items of different data types. To define a structure in C++ we use the struct keyword, and variables reside in a structure known as Structure members.

    Create a Structure

    The struct keyword is used to create the structure and the items it consists known as Members or Fields of Structures.
    Structure Syntax:
    struct structureName
    {
    member1;
    member2;
    member3;
    };
    There are two types of Members in the C++ structure:
    • Data Member
    • Member Function
    Data Members are the variables which are inside the Structure block. Member Functions are the functions inside the Structure Block.

    Accessing Structure Member

    Once we have defined a structure with its members, we need to access it all the data, and to access the members of the structure we use the member access operator(.), which is a dot symbol . First, we need to define the Structure variable using the Structure Name and then using the structure variable and member access operator we can access any member of the structure. Example
    #include <iostream>
    #include <cstring>
    using namespace std;
    
        //defining a structure
    struct Students 
       {
          char  full_name[50]; // data members
          int  age;
          char  subject[100];
       };
    
    int main()
        { 
             struct Students sam; // sam is the structure variable
             strcpy(sam.full_name, "Sam Wick");  // full_name is the string data type so we have used the string funciton
             sam.age =10;
             strcpy(sam.subject,"Science");
             cout<<"The Full name of sam is: "<<sam.full_name;
             cout<<endl;
             cout<<"Sam Age is: "<< sam.age;
             return 0;
        }
    Output
    The Full name of sam is: Sam Wick
    Sam Age is: 10
    Behind the Code In this example first, we define a structure outside the main() function. And then using the struct keyword and Students name we create a new variable sam. Now sam is a structure student type data structure which can access any data member of Student structure. We can also make more than one Structure variable if we want.

    Structure Functions

    We can also define a user-defined function inside the structure block, and like we access the data member we can access the Member Functions of the Structure. Example
     #include <iostream>
    #include <cstring>
    using namespace std;
    
        //defining a structure
        struct Students 
             {
             char  full_name[50];
             int  age;
             char  subject[100];
          
             void show_detail()
                {
                    cout<<" Student Name is: "<<full_name;
                    cout<<endl;
                    cout<<"Student age is: "<<age;
                    cout<<endl;
                    cout<<"Student subject is: "<<subject;
                 }
             };
    
    int main()
        {
            struct Students sam; // sam is the structure variable
            strcpy(sam.full_name, "Sam Wick");  // full_name is the string data type so we have used the string funciton
            sam.age= 10; // setting the age of sam varible
            strcpy(sam.subject,"Science");  // setting the sam variable subject name
       
            //print student detail
            sam.show_detail(); // calling the structure member function
            return 0;
        }
    Output
    Student Name is: Sam Wick
    Student age is: 10
    Student subject is: Science
    Behind the Code In this example, we have defined a member function inside the struct Students block, and using the sam.show_detail() statement we called that function inside the main() function.

    Structure Pointer

    Like other basic data types, we can also use pointers with structure. But if we use a pointer with the structure we use (->) symbol as the member access operator instead of the dot (.) operator. Example
    #include <iostream>
    #include <cstring>
    using namespace std;
    
    //defining a structure
    
    struct Students 
       {
         char  full_name[50];
       };
     
    int main()
        {
               struct Students sam; // sam is the structure variable
                
               strcpy(sam.full_name, "Sam Wick");
    
               struct Students* sam_wick = &sam ; // the pointer variable sam_wick is also a structure variable
             
               cout<<"The full name of Student is: "<< sam_wick->full_name;  // here we have used -> operator because sam_wick is a structure pointer variable
               return 0;
        }
    Output
    The full name of Student is: Sam Wick
    Behind the Code Here sam is the structure variable, like other variables it also stores at some memory location. Now, here we have used the struct Students* sam_wick = &sam ; statement, which basically signifies that sam_wick is a pointer variable which holds the address of sam variable. We all know that pointer variable should be of similar data type which addresses it holding that’s why sam_wick is also a struct Students variable. In structure, if we want to access the data member of the structure using a pointer variable than we use the -> symbol as a member access operator.

    Structure Quick summary

    • The structure is a data structure which can hold different elements of different data types.
    • We can also define functions inside the structure variable.
    • The variable inside the structure known as Data members whereas the functions known as Member functions.
    • To access the data member and Member functions of the structure we use the structure variable with the member access operator.
    People are also reading: