Structures

  • Why structures?
  • As you know 'C' language provide us with variable to store the single data ,Array for storing related type of data,then Why structures ?
  • Because, In real life there is a situation where we have store a data which is logical related to each other but they are not of same type.
  • They are related but ,Data contains Dissimilar information,which means each data is of different datatype.
  • For example you have to store a student information:
    then attributes of student will be Name ,roll_no,class,div,address,etc then how can you store this data by Using int ,char or by using simple array,So this not possible by predefined datatypes.
  • Now , at this type of situation structures comes in action.

What are structures?

  • Structures are Programmer define Data types.
  • Structure is a collection of logically related data of different data types which are grouped together under a single name.
  • Syntax of defining structure.

    Syntax:
    struct structure_name
    {
        datatype  member1; // each item  in structure is member
        datatype  member2;
        ......
        ......
    };
    

    Note:struct is a keyword.
    while defining structure you have to use that keyword.

  • Let's define our first structure.
    We will define structure for storing data of student.


    Definition:
    struct student  //structure name
    {
        int  rollno; //member1 ,integer variable which will store,
                     //student roll no.
                     
        char  name[20]; //member2 a character array of 20 character
                        //which will store name of student.
                        
        char  div;  //member3 a character variable,
                  //which will store division of student.
    };
    

  • Q:Define a structure employee and it should have employee name,employee id,employee designation,and employee salary as it's members.

    Definition:
    struct employee  //structure name
    {
        int employee_id;    //member1 int variable which,
                            //will store employee id
        
        char employee_name[20];   //member2, character array which will,
                                  //store name of employee.
        
        char employee_desig[20];   //member3,character array which will,
                                  //store designation of employee.
        
        int employee_salary;   //member4, int variable which will,
                                 //store employee salary.
        
        
    };
    

  • Q:Define a structure Book and it should have book name,book id ,book price,and author name as an member in it.

    Definition:
    struct Book  //structure name
    {
        int Book_id; //member1 int variable which,
                        //will store Book id
        
        char Book_name[20];//member2, character array which will,
                              //store name of book.
        
        char author_name[20];//member3,character array which will,
                               //store name of author.
        
        int Book_price; //member4, int variable which will,
                            //store price of book.
        
        
    };
    

  • The structure template ends with the semicolon ';'.
  • The members of structure are enclosed in parenthesis {}.

  • Now we have learned how to define a structure ,so let's use it.
  • But before using we have to declare the structure.
  • let's see syntax of declaring structure variable
  • Syntax:
    struct structure_name  variable_name;
    

  • struct keyword is used while declaring structure variable.
  • Above we have defined student data type,so declare a variable of student data type.

    Declaration:
    void main()
    {
        
     struct student s1; //s1 is a variable of student type.
                       //as student is a  datatype  which is,
                         //define by us.
                
    }                     
    




  • Q:declare a variable 'emp1' of employee datatype.
    employee datatype has define above.

    Declaration:
    void main()
    {
        
     struct employee emp1; //emp1 is a variable of employee type.
                       //as employee is a  datatype  which is,
                         //define by us.
                
    }                     
    




  • Q:declare a variable 'b1' of Book datatype.
    Book datatype has define above.

    Declaration:
    void main()
    {
        
     struct Book b1; //b1 is a variable of Book data type.
                       //as Book is a  datatype  which is,
                         //define by us.
                
    }                     
    








Further Concept:



People Also Searched: