C Array of Structure, C Array within Structure

Advertisements

Array and Structure in C

We can use structure and array as :

  • Array of Structure
  • Array within Structure

Array of Structure

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. As we know, an array is a collection of similar type, therefore an array can be of structure type.

Syntax for declaring structure array


              struct struct-name
              {
                     datatype var1;
                     datatype var2;
                     - - - - - - - - - -
                     - - - - - - - - - -
                     datatype varN;
              };

              struct struct-name obj [ size ];

Example for declaring structure array


       #include<stdio.h>

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

       void main()
       {
              int i;
              struct Employee Emp[ 3 ];         //Statement   1

              for(i=0;i<3;i++)
              {

              printf("\nEnter details of %d Employee",i+1);

                    printf("\n\tEnter Employee Id : ");
                    scanf("%d",&Emp[i].Id);

                    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("\nDetails 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);

       }


   Output :

              Enter details of 1 Employee
                        Enter Employee Id : 101
                        Enter Employee Name : Suresh
                        Enter Employee Age : 29
                        Enter Employee Salary : 45000

              Enter details of 2 Employee
                        Enter Employee Id : 102
                        Enter Employee Name : Mukesh
                        Enter Employee Age : 31
                        Enter Employee Salary : 51000

              Enter details of 3 Employee
                        Enter Employee Id : 103
                        Enter Employee Name : Ramesh
                        Enter Employee Age : 28
                        Enter Employee Salary : 47000

              Details of Employees
                        101       Suresh      29      45000
                        102       Mukesh     31      51000
                        103       Ramesh    28      47000

				

In the above example, we are getting and displaying the data of 3 employee using array of object. Statement 1 is creating an array of Employee Emp to store the records of 3 employees.

Array within Structure

As we know, structure is collection of different data type. Like normal data type, It can also store an array as well.

Syntax for array within structure


              struct struct-name
              {
                     datatype var1;                    // normal variable
                     datatype array [size];          // array variable
                     - - - - - - - - - -
                     - - - - - - - - - -
                     datatype varN;
              };

              struct struct-name obj;

Example for array within structure


       struct Student
       {
              int Roll;
              char Name[25];
              int Marks[3];              //Statement 1 : array of marks
              int Total;
              float Avg;
       };

       void main()
       {
              int i;
              struct Student S;

              printf("\n\nEnter Student Roll : ");
              scanf("%d",&S.Roll);

              printf("\n\nEnter Student Name : ");
              scanf("%s",&S.Name);

              S.Total = 0;

              for(i=0;i<3;i++)
              {
                     printf("\n\nEnter Marks %d : ",i+1);
                     scanf("%d",&S.Marks[i]);

                     S.Total = S.Total + S.Marks[i];
              }

              S.Avg = S.Total / 3;

              printf("\nRoll : %d",S.Roll);
              printf("\nName : %s",S.Name);
              printf("\nTotal : %d",S.Total);
              printf("\nAverage : %f",S.Avg);
       }

   Output :

              Enter Student Roll : 10
              Enter Student Name : Kumar
              Enter Marks 1 : 78
              Enter Marks 2 : 89
              Enter Marks 3 : 56

              Roll : 10
              Name : Kumar
              Total : 223
              Average : 74.00000

				

In the above example, we have created an array Marks[ ] inside structure representing 3 marks of a single student. Marks[ ] is now a member of structure student and to access Marks[ ] we have used dot operator(.) along with object S.

Related topics

Advertisement