Structure in C, Array of Structure, Structure within Structure

Advertisements

Structure in C

Structure is commonly reffered to as user-defined data type. Structure is similar to an array but the only difference is that array is collection of similar data type onthe other hand structure is collection of different data type. A structure can contain any data type including array and another structure as well. Each variable declared inside structure is called member of structure.

Structure declaration

Declaration of structure must start with the keyword struct followed by the structure name and structure's member variables are declared within braces.

Syntax for declaring structure


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

			

Example for declaring structure


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

Accessing the structure members

We have to create an object of structure to access its members. Object is a variable of type structure. Structure members are accessed using the dot operator(.) between structure's object and structure's member name.

Syntax for creating object


              struct struct-name obj;

Example for creating object & accessing structure members


       #include<stdio.h>


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

       void main()
       {

              struct Employee E;             //Statement  1

                    printf("\nEnter Employee Id : ");
                    scanf("%d",&E.Id);

                    printf("\nEnter Employee Name : ");
                    scanf("%s",&E.Name);

                    printf("\nEnter Employee Age : ");
                    scanf("%d",&E.Age);

                    printf("\nEnter Employee Salary : ");
                    scanf("%ld",&E.Salary);

                    printf("\n\nEmployee Id : %d",E.Id);
                    printf("\nEmployee Name : %s",E.Name);
                    printf("\nEmployee Age : %d",E.Age);
                    printf("\nEmployee Salary : %ld",E.Salary);


       }

   Output :

              Enter Employee Id : 1
              Enter Employee Name : Kumar
              Enter Employee Age : 29
              Enter Employee Salary : 45000

              Employee Id : 1
              Employee Name : Kumar
              Employee Age : 29
              Employee Salary : 45000

Statement 1 is creating an object E of Employee type.

Initialization of structure

Like normal variable structures can be initialized at the time of declaration. Initialization of structure is almost similar to initializing array. The structure object is followed by equal sign and the list of values enclosed in braces and each value is separated with comma.

Example for declaring & initializing structure at same time


       #include<stdio.h>


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

       void main()
       {

              struct Employee E = {2,"Suresh",35,35000};

                    printf("\n\nEmployee Id : %d",E.Id);
                    printf("\nEmployee Name : %s",E.Name);
                    printf("\nEmployee Age : %d",E.Age);
                    printf("\nEmployee Salary : %ld",E.Salary);

       }

   Output :

              Employee Id : 1
              Employee Name : Kumar
              Employee Age : 29
              Employee Salary : 45000

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.

Nested Structure in C

When a structure contains another structure, it is called nested structure. For example,we have two structures named Address and Employee. To make Address nested to Employee, we have to define Address structure before and outside Employee structure and create an object of Address structure inside Employee structure.

Syntax for structure within structure or nested structure


              struct structure1
              {
                     - - - - - - - - - -
                     - - - - - - - - - -
              };

              struct structure2
              {
                     - - - - - - - - - -
                     - - - - - - - - - -
                     struct structure1 obj;
              };

Example for structure within structure or nested structure


       #include<stdio.h>

       struct Address
       {
              char HouseNo[25];
              char City[25];
              char PinCode[25];
       };

       struct Employee
       {
           int Id;
           char Name[25];
           float Salary;
           struct Address Add;
       };

       void main()
       {
              int i;
              struct Employee E;

              printf("\n\tEnter Employee Id : ");
              scanf("%d",&E.Id);

              printf("\n\tEnter Employee Name : ");
              scanf("%s",&E.Name);

              printf("\n\tEnter Employee Salary : ");
              scanf("%f",&E.Salary);

              printf("\n\tEnter Employee House No : ");
              scanf("%s",&E.Add.HouseNo);

              printf("\n\tEnter Employee City : ");
              scanf("%s",&E.Add.City);

              printf("\n\tEnter Employee House No : ");
              scanf("%s",&E.Add.PinCode);

              printf("\nDetails of Employees");
              printf("\n\tEmployee Id : %d",E.Id);
              printf("\n\tEmployee Name : %s",E.Name);
              printf("\n\tEmployee Salary : %f",E.Salary);
              printf("\n\tEmployee House No : %s",E.Add.HouseNo);
              printf("\n\tEmployee City : %s",E.Add.City);
              printf("\n\tEmployee House No : %s",E.Add.PinCode);

       }


   Output :

                     Enter Employee Id : 101
                     Enter Employee Name : Suresh
                     Enter Employee Salary : 45000
                     Enter Employee House No : 4598/D
                     Enter Employee City : Delhi
                     Enter Employee Pin Code : 110056

              Details of Employees
                     Employee Id : 101
                     Employee Name : Suresh
                     Employee Salary : 45000
                     Employee House No : 4598/D
                     Employee City : Delhi
                     Employee Pin Code : 110056

				

Structure and Function in C

Using function we can pass structure as function argument and we can also return structure from function.

Passing structure as function argument

Structure can be passed to function through its object therefore passing structure to function or passing structure object to function is same thing because structure object represents the structure. Like normal variable, structure variable(structure object) can be pass by value or by references / addresses.

Passing Structure by Value

In this approach, the structure object is passed as function argument to the definition of function, here object is reperesenting the members of structure with their values.

Example for passing structure object by value


       #include<stdio.h>

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

       void Display(struct Employee);
       void main()
       {
              struct Employee Emp = {1,"Kumar",29,45000};

              Display(Emp);

       }

       void Display(struct Employee E)
       {
                    printf("\n\nEmployee Id : %d",E.Id);
                    printf("\nEmployee Name : %s",E.Name);
                    printf("\nEmployee Age : %d",E.Age);
                    printf("\nEmployee Salary : %ld",E.Salary);
       }

   Output :

              Employee Id : 1
              Employee Name : Kumar
              Employee Age : 29
              Employee Salary : 45000

				

Passing Structure by Reference

In this approach, the reference/address structure object is passed as function argument to the definition of function.

Example for passing structure object by reference


       #include<stdio.h>

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

       void Display(struct Employee*);
       void main()
       {
              struct Employee Emp = {1,"Kumar",29,45000};

              Display(&Emp);

       }

       void Display(struct Employee *E)
       {
                    printf("\n\nEmployee Id : %d",E->Id);
                    printf("\nEmployee Name : %s",E->Name);
                    printf("\nEmployee Age : %d",E->Age);
                    printf("\nEmployee Salary : %ld",E->Salary);
       }

   Output :

              Employee Id : 1
              Employee Name : Kumar
              Employee Age : 29
              Employee Salary : 45000

				

Function Returning Structure

Structure is user-defined data type, like built-in data types structure can be return from function.

Example for passing structure object by reference


       #include<stdio.h>

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

       Employee Input();            //Statement   1
       void main()
       {
              struct Employee Emp;

              Emp = Input();

              printf("\n\nEmployee Id : %d",Emp.Id);
              printf("\nEmployee Name : %s",Emp.Name);
              printf("\nEmployee Age : %d",Emp.Age);
              printf("\nEmployee Salary : %ld",Emp.Salary);

       }

       Employee Input()
       {
              struct Employee E;

                    printf("\nEnter Employee Id : ");
                    scanf("%d",&E.Id);

                    printf("\nEnter Employee Name : ");
                    scanf("%s",&E.Name);

                    printf("\nEnter Employee Age : ");
                    scanf("%d",&E.Age);

                    printf("\nEnter Employee Salary : ");
                    scanf("%ld",&E.Salary);

              return E;             //Statement   2
       }

   Output :

              Enter Employee Id : 10
              Enter Employee Name : Ajay
              Enter Employee Age : 25
              Enter Employee Salary : 15000

              Employee Id : 10
              Employee Name : Ajay
              Employee Age : 25
              Employee Salary : 15000

				

In the above example, statement 1 is declaring Input() with return type Employee. As we know structure is user-defined data type and structure name acts as our new user-defined data type, therefore we use structure name as function return type.

Input() have local variable E of Employee type. After getting values from user statement 2 returns E to the calling function and display the values.

Related topics

Advertisement