ARRAY OF STRUCTURES - C-Tutorial

Latest

Thursday, 20 October 2016

ARRAY OF STRUCTURES

ARRAY OF STRUCTURES

ARRAY OF STRUCTURES

Structure is collection of different data type. An object of structure represents a single record in memory, if we want more than one record of structure type, we have to create an array of structure or object.

            The general format of defining array of structures is:

            Syntax:           struct Tag
                                    {
                                                datatype member1;
                                                datatype member2;
                                                            - - - - -
                                                            - - - - -
                                                datatype membern;
                                    } Variable[size];

Note: size specifies the no.of structure variable.

#include<stdio.h>

       struct Employee
       {
              int Id;
              char Name[25];
              int Age;
              long Salary;
       }emp[3];


       void main()
       {
              int i;
              for(i=0;i<3;i++)
              {
              printf("\nEnter details of %d Employee",i+1);
                    printf("\n\tEnter Employee Id : ");
                    scanf("%d",&emp[i].Id);
                    fflush(stdin);
                    printf("\n\tEnter Employee Name : ");
                    scanf("%s",&emp[i].Name);
                    printf("\n\tEnter Employee Age : ");
                    scanf("%d",&emp[i].Age);
                    printf("\n\tEnter Employee Salary : ");
                    scanf("%ld",&emp[i].Salary);
              }
              printf("\n Details of Employees");
              for(i=0;i<3;i++)
              printf("\n%d\t%s\t%d\t%ld",emp[i].Id,emp[i].Name,emp[i].Age,emp[i].Salary);

       }

No comments:

Post a Comment