• Main Menu
  • Structures


    Structure is a collection of variables referenced under a common name. Sometimes, some logically related elements need to be treated under one single unit. For instance, the elements that store student information (e.g., rollnumber, name, class, marks and grade) need to be processed together under one roof.

    Defining a structure:

    struct tag-name 
    { 
        Member1 ; 
        Member2 ; 
        Member3 ; 
    } ;
    • struct is a keyword required to define a structure.
    • tag-name is the name that identifies the structure.
    • member1, member2 and member3 are the members of the structure.

    Complex data types can be constructed from fundamental data types.

    Example:

    struct account 
    { 
        int accno ; 
        char name[40] ; 
        float balance ; 
    } oldcustomer, newcustomer ;

    oldcustomer and newcustomer are structure variables of type account.

    struct account 
    { 
        int accno ; 
        char name[40] ; 
        float balance ; 
    } customer[100] ;

    Here, customer is a 100-element array of structures. Each element of customer is a separate structure of type account. Each customer includes all the members.

    Variable declaration:

    struct card c1, c2 ;

    Structures can contain any C++ data type, including arrays, pointers or other structures.

    Referencing Structure Elements

    Once a structure has been defined, its members can be accessed with the use of dot (.) operator.

    //Syntax: 
    structure-name.element-name ; 
    //Example: 
    oldcoustomer.name = “Albert” ; 
    oldcoustomer.accno = 372 ;

    The structure variable name followed by a period (.) and the element name is the name of the specific structure variable.

    Initialization of Structure

    Structure elements can be initialized in 2 ways,

    		//By initializing every element individually
    		oldcoustomer.name = ”Albert” ;
    		oldcoustomer.accno = 372 ;
    		oldcustomer.balance = 58880.50 ;
    
    		//By the notation which is used for the initialization of arrays
                     account oldcustomer = { 372 , ”Albert” , 58880.50 } ;

    Nested Structure

    A structure element may be either a complex or simple data-type. The simple data-types are any of the fundamental data-types such as int, char, float, double. However, a structure may have elements that are complex i.e. array, structure, etc. Thus, an element of a structure may even be an array or a structure itself. A structure which is a member of another structure, is known as a nested structure.

    Example:

    struct address 
    { 
        int houseno ; 
        char area[26]; //array as an element in a structure 
        char city[26] ; 
        char state[26] ; 
    }; 
    struct employee 
    { 
        int empno ; 
        char name[26] ; 
        char design[26] ; 
        address addr ; //structure as an element in a structure 
        float basic ; 
    } worker ;

    Program 1:

    Accepts data in the structure named student and display the contents.

    #include <iostream.h>
    int main()
    {
        struct student 
        { 
            int rollno; 
            char name[10] ; 
            int marks;; 
        } stud1, stud2 ;
    
        cout << "Enter the data for the first student: \n" ;
        cout << "Rollno. : \n" ;
        cin >> stud1.rollno;
        cout << "name. : \n" ;
        cin >> stud1.name;
        cout << "marks. : \n" ;
        cin >> stud1.marks;
    
        cout << "Enter the data for the second student: \n" ;
        cout << "Rollno. : \n" ;
        cin >> stud2.rollno;
        cout << "name. : \n" ;
        cin >> stud2.name;
        cout << "marks. : \n" ;
        cin >> stud2.marks;
    
        cout<<"\n"<<stud1.rollno<<"\n"<<stud1.name<<"\n"<<stud1.marks<<"\n";
        cout<<"\n"<<stud2.rollno<<"\n"<<stud2.name<<"\n"<<stud2.marks<<"\n";
    }

    Output:

    Enter the value for first student:
    Rollno. : 568
    name    : mark
    marks   : 687
    
    Enter the value for second student:
    Rollno. : 567
    name    : alex
    marks   : 645
    
    568
    mark
    678
    
    567
    alex
    645

    Program 2:

    Accepts data into an array of stuture and display the contents.

    #include <iostream.h>
    struct employee 
        { 
            int rollno; 
            char name[10] ; 
            int marks;; 
        } stud[5];
    void main()
    {
        int i;
        for(int i=0; i<= 4; i++) 
        {
            cout << "Enter the data for "<< i+1 <<"  student: \n" ;
            cout << "Rollno. : \n" ;
            cin >> stud[i].rollno;
            cout << "name. : \n" ;
            cin >> stud[i].name;
            cout << "marks. : \n" ;
            cin >> stud[i].marks;
        }
    
        for(int i=0; i<= 4; i++) 
        {
            cout << "\n The data for "<< i+1 <<"  student: \n" ;
            cout << "\n Rollno. : "<<stud[i].rollno; ;
            cout << "\n name. : "<<stud[i].name; ;
            cout << "\n marks. : "<<stud[i].marks; ;
        }
    }

    Output:

    Enter the data for 1 student:
    Rollno. : 1
    name    : matt
    marks   : 419
    
    Enter the data for 2 student:
    Rollno. : 2
    name    : alex
    marks   : 389
    
    Enter the data for 3 student:
    Rollno. : 3
    name    : john
    marks   : 456
    
    Enter the data for 4 student:
    Rollno. : 4
    name    : kyle
    marks   : 378
    
    Enter the data for 5 student:
    Rollno. : 5
    name    : ross
    marks   : 402
    
    The data for 1 student:
    Rollno. : 1
    name    : matt
    marks   : 419
    
    The data for 1 student:
    Rollno. : 2
    name    : alex
    marks   : 389
    
    The data for 1 student:
    Rollno. : 3
    name    : john
    marks   : 456
    
    The data for 1 student:
    Rollno. : 4
    name    : kyle
    marks   : 378
    
    The data for 1 student:
    Rollno. : 5
    name    : ross
    marks   : 402

    Got Something To Say:

    Your email address will not be published. Required fields are marked *

    C
    } 237 queries in 0.306 seconds.