Structure

What is Structure in C?

Structure is user defined datatype in C language which allows us to combine data of different types together. structure somewhat similar to the array. but array is the collection of the homogeneous elements. and structure consist of the collection of the heterogeneous elements.

Structure Defination in C(Declaration)

For creating a structure struct keyword is used.

Syntax of structure in C:
             struct structure_name
             {
              data_type member1;
              data_type member2;
              .
              .
              data_typen memebern;
             };   

We can create the structure for a student as mentioned above as:

Example:
             struct student
            {
             char name[50];
             int Roll_No;
             float Marks;
            };
         

Structure variable declaration

When a structure is defined, it creates a user-defined type but, no storage is allocated. For the above structure of student, variable can be declared as:

             struct student
             {
              char name[50];
              int Roll_No;
              float Marks;
             };

             inside a main function
             struct person s1, s2, s[20];  

Another way of creating structure

              struct student
              {
               char name[50];
               int Roll_No;
               float Marks;
              }s1 ,s2 ,s[20];
          

In both cases, 2 variables s1, s2 and array p having 20 elements of type struct person are created.

Accessing members of a structure

There are two types of operators are used to accessing the member of structure

  1. Member operator(.)
  2. Structure pointer operator(->)

Any member of a structure can be accessed as:

structure_variable_name.member_name

Suppose we want access the Marks for variable s2. then it can be accessed as:

s2.Marks

Initialization of Structure

There are two types of the structure initialization:

  1. Compile time initialization
  2. Run time initialization
1.compile time initialization
Sample program to implement structure at compile time
         
                  
             #include<stdio.h> 
             #include<conio.h> 
         
              struct student
              {
              char name[20];
              int rollno;
              float marks;
              }s{"xyz", 4, 560};

              void main()
              {
              clrscr();
              printf("\nStudent Name\tRollno\tMarks\n");
              printf("%s\t%i\t%f", s.name, s.rollno, s.marks);
              printf("\n");
              printf("%s\t%i\t%f",s.name,s.rollno,s.marks);
              getch();
              }                   

Output:

            Student Name   Rollno    Marks
              xyz            4        560        
2.Run time initialization
Sample program to implement structure at run time
         
                  
          #include<stdio.h> 
          #include<conio.h> 
         
           struct student
           {
           char name[20];
           int rollno;
           float marks;
           };
           void main()
           {
           struct student s;
           clrscr();
           printf("Enter student Name, Rollno, Marks:\n");
           scanf("%s%i%f", s.name, &s.rollno, &s.marks);
           printf("\nStudent Name\tRollno\tMarks\n");
           printf("%s\t%i\t%f", s.name, s.rollno, s.marks);
           getch();
           }
         

Output:

             Enter student name, Rollno, marks
             xyz
             4
             560
             Student Name   Rollno    Marks
             xyz            4        560         
Sample program of structure
1.Define structure Cricket_Team having element player_name, age, score_in_ODI.
- Declare and do Initialization the structure for 5 cricketers by taking input at run time
- Display the information of all cricketers.
         
     
             #include<stdio.h> 
             #include<conio.h> 
         
             struct cricket
              {
               char name[20];
               int age;
               int score;
              };
              void main()
              {
               int i;
               struct cricket c[2];
               clrscr();
               for(i=0;i<2;i++)
               {
                printf("Enter the name of the player:");
                scanf("%s",c[i].name);
                printf("Enter age:");
                scanf("%d",&c[i].age);
                printf("Enter score in ODI:");
                scanf("%d",&c[i].score);
               }
               printf("Entered data is:\n");
               for(i=0;i<2;i++)
               {
                printf("Name of the player:%s\n",c[i].name);
                printf("Age:%d\n",c[i].age);
                printf("Score in ODI:%d\n",c[i].score);
               }
              getch();
            }
     

Output:

        Enter the name of the player:
        abc
        Enter age:
        20
        Enter score in ODI:
        750

        Enter the name of the player:
        xyz
        Enter age:
        22
        Enter score in ODI:
        678

        Entered data is:
        Name of the player:abc
        Age:20 
        Scorein ODI:750

        Entered data is:
        Name of the player:xyz
        Age:22
        Scorein ODI:678
     

Passing a structure to function

There are three methods to pass structure to function

  1. Pass each member of structure as actual parameter to function call.
  2. Passing copy of the entire structure to the called function.
  3. Pass Pointer of structure as argument to function.
1.Pass each member of structure as actual parameter to function call.
Sample program
         
     
         #include<stdio.h> 
         #include<conio.h> 
         
         struct student
         {
          char name;
          int rollno;
          float marks;
         };
         struct student std_func(char, int, float);

         int main()
         {
          struct student s1 = {'xyz', 10, 570};
          clrscr();
          std_func(s1.name, s1.rollno, s1.marks);
          getch();
         }

         struct student std_func(char name, int rollno, float marks)
         {
          printf("Name\tRoll No.\tMarks\n");
          printf("%c\t%d\t\t%f", name, rollno, marks);
         }
     

Output:

                Name Roll No. Marks
                xyz   10   570.000000    
2.Passing copy of the entire structure to the called function.
Sample program
         
     
         #include<stdio.h> 
         #include<conio.h> 
         
         void modify(struct student s);
         struct student
         {
          int roll_no;
          float marks;
         };

         void main()
         {
          struct student s;
          clrscr();
          printf("\nEnter roll_no and marks:\n");
          scanf("%d%f", &s.roll_no, &s.marks);
          modify(s);
          printf("\nafter modificattion in main:\n");
          printf("%d\t%2f", s.roll_no, s.marks);
          getch();
         }

         void modify(struct student s)
         {
          printf("\nmodify the marks");
          scanf("%f",&s.marks);
          printf("\nafter modificattion in function:\n");
          printf("%d\t%2f", s.roll_no,s.marks);
         }
     

Output:

                Enter roll_no and marks:
                2
                70.40
                modify the marks89.90
                after modificattion in function:
                1 89.900000
                after modificattion in main:
                1 70.400000  
3.Pass Pointer of structure as argument to function.
Basis for Comparison Array Structure
Basic An array is a collection of variables of same data type. A structure is a collection of variables of different data type.
Syntax Syntax type array_name[size] struct sruct_name{
type element1;
type element1;
.
.
} variable1, variable2, . .;
Memory Array elements are stored in contiguous memory location. Structure elements may not be stored in a contiguous memory location.
Access Array elements are accessed by their index number. Structure elements are accessed by their names.
Operator Array declaration and element accessing operator is "[ ]" (square bracket). Structure element accessing operator is "." (Dot operator).
Pointer Array name points to the first element in that array so, array name is a pointer. Structure name does not point to the first element in that structure so, structure name is not a pointer.
Size Every element in array is of same size. Every element in a structure is of different data type.
Keyword There is no keyword to declare an array. "struct" is a keyword used to declare the structure.
User-defined Arrays are not user-defined they are directly declared Structure is a user-defined datatype.
Examples For Practice
  1. Write a program to
    -Define the structure student having member’s rollno, name, address, marks.
    - Initialize the structure for single student by taking input at compile time
    - Display the information
  2. Write a program to
    -Definethe structure book having members book_id ,title, author, price.
    - Initialize the structure for single book by taking input at run time
    - Display the information