Structures

Sunday 14 August 2016

Structure:

  • Structure is a collection of related data elements of different datatypes. 
         Syntax:        struct   tag-name
                             {
                                 datatype    m1;
                                 datatype    m2;
                                 -------------------
                               };
         Example:
                          struct   employee
                            {
                               char  name[20];
                               int     id;
                               float  salary;
                            };
                               
from this example   Struct  is a keyword tells the compiler that structure is defined.
                              Tag-name  is name of structure.
                              m1,m2  are called members of the structure.
                              Finally there should be semicolon at the end of closing brace.

  • Memory is not allocated to members when structure is defined.

Structure Declaration:

  • The structures are defined and declared before they are used.
              Syntax:       Struct     tag-name    list-of-variables;
          
             Example      struct   employee   e1,e2;

Here structure definition and declaration can be shown as follows:
                                 
                                          Struct    employee
                                         { 
                                             char   name[20];
                                              int    id;
                                             float  salary;
                                         };
                                          Struct  employee  e1,e2;

Calculating the Size of Structure:

  • From the above employee example,The number of bytes allocated for e1 :
                                        20 bytes for name of employee.
                                         2  bytes for id of employee 
                                         4  bytes for salary of employee.
                                   --------
                          Total     26 bytes allocated for e1     

  • Similarly for e2 26 bytes allocated.
finally the Total memory allocated  for employee structure is 52 bytes(26+26).

Structure Initialization:

  • Structure initialization can be done by following syntax.
              Syntax:    struct   employee
                              {
                                char  name[20];
                                int    id;
                                float  salary;
                               };
                             struct employee e1={"a",1000,1234};

  • The values are assigned to various members in the order specified from first.

How To access structures:

  • The members present inside structures can be accessed by variable name followed by  . (period)
           syntax:      variable-name . member
           example      e1.name
                              e1.id
    

program on over view of structures:


                































You can also refer
----------------------------------------------------------------------------------------------------------

No comments:

Post a Comment