Structure Definition with Example

Introduction - Structure

Array can be used to represent a group of data items that belongs to the same type. Such as int or float so need to represent a collection of data items of different types using a single name at that time array can not be used.
 
 C supports a constructed data type known as structure. A  Structure is a collection of one or more variables grouped as a unit under a single name for easy processing.
 
 A structure is a convenient tool for handling a group of logically related data items.For example , it can be used to represent a set of attributes such as Employee_ name,Department,Salary etc.

Defining a Structure

The structure consists of a number of data items grouped together but unlike array , each data item in the group need not be of the same type. It may consist data of different types
 
The syntax for declaring the structure is as follows:

          struct tag_name
          {
                   Data_type member 1;
                   Data_type member 2;
          };

 As shown in above syntax struct is a keyword.
Tag_name is the name that identifies structure of this type.
member1,member2 …are individual member declaration with their data type
The struct template is terminated with a semicolon. The entire definition is considered as a statement.
 
Ex
          struct employee
          {
                   int no;
                   char name[15];
                   float salary;
          };

Here, struct  declares the structure to hold detail of three variables. These fields are  called structure elements or member. Each member may belong to different type of data.

Declaring Structure variables:

After defining a structure data type need to declare one or more variables to be of that type.
A structure variable declaration is similar to the declaration of variable of any other data types.
It includes the following elements.
·                                                                           struct keyword
·                                                                           the structure tag name
·                                                                           List of variable name separated by commas.
·                                                                           a terminating semicolon.
 
Ex
struct employee
          {
                   int no;
                   char name[15];
                   float salary;
          };
         
          struct employee e1,e2,e3;
 
With using this method  structure variable can be  declared any where in the program.The members of a structure themselves are not variables.They do not occupy any memory until they are associated with the structure variable such as e1.
 
It is also allowed to combine both the structure definition and variables declaration in one statement.
 
Ex, struct employee
          {
                   int no;
                   char name[15];
                   float salary;
          }e1,e2,e3; 

The use of tag name is optional.
Ex Struct
          {
                   int no;
                   char name[15];
                   float salary;
          }e1,e2,e3;

Here e1,e2,e3 are declared as a  structure variable but doesn’t include a tag name for later use in declarations.
 
The keyword typedef is also using to define a structure. such as
 
typedef struct
{
    type member1;
    type member2;
    type member3;
}type_name;

Here, the type_name represents structure definition associated with it and therefore can be used to declare structure variable.
ex, type_name var1,var2,var3....;
         
Accessing Member of Structure

A member of a structure can be accessed and used individually in a problem.For thet consider following syntax:

                   struct_var_name.member_name

Here, the struct_var_name is the name of structure variable and member name is the name of variable which is member of structure.It can be accessed using  the dot(.).
Ex
          struct employee
          {
                   int no;
                   char name[15];
                   float salary;
          };

          struct employee e1;

          e1.no=5;
          strcpy(e1.name,”xyz”);
          e1.salary=1000;
         
          The structure is mainly used to store a record that contain different data type.

Structure Initalization
A structure can be initialized by specified the values of data members just after the declaration of structure.


Ex
struct emp
{
          int no;
          char name[15];
          floalt salary;
};
void main()
{    //initialization
          struct emp e1={1,”xyz”,5000};
          struct emp e2={2,”abc”,6000};

          printf(“\n No         Name           Salary”);
          printf(“\n%d         %s              %f”,e1.no,e1.name,e1.salary);
          Printf(“\n%d         %s              %f”,e2.no,e2.name,e2.salary);
}

Post a Comment

Previous Post Next Post