Array of Structure

  • Till now we have seen array of basic data types like array of int ,array of float ,array character etc.
  • But now we will see how to make array of structure variable.
  • So,let's define a structure

Let's define a structure Student with name ,rollno, div as it's attribute.

Definition:
struct student  //structure name
{
    int  rollno; //member1 ,integer variable which will store,
                 //student roll no.
                 
    char  name[20]; //member2 a character array of 20 character
                    //which will store name of student.
                    
    char  div;  //member3 a character variable,
              //which will store division of student.
};

Syntax:Declaration of structure array
struct  structure_name   structure_variable_name[size_of_array];

Declaration of structure student array
void main()
{
//declaration of student array.
struct student s[10];

}

Initialization of Structure array:
void main()
{
//declaration of student array.
struct student s[3] = {{1,"Roy",'A'}, //data of first student
                       {2,"Jason",'B'},//data of second student
                       {3,"Tom",'A'}};//data of third student


}

How to Access members of structure array.

  • As,you know every array element has an unique index number,Which starts from 0 index.
  • Means first array element wil have index 0,second array element will have index 1 and So on...
  • Let's see memory view of structure array,by looking it you will get an great understanding of array structure
 

Syntax | Accessing member of structure array
struct_variable_name[index_number].member_name;

Let's access the rollno of each student.

Accessing:Rollno of first student
int roll_1;
roll_1  =  s[0].rollno;

Accessing:Rollno of second student
int roll_2;
roll_1  =  s[1].rollno;

Accessing:Rollno of Third student
int roll_3;
roll_1  =  s[2].rollno;

This how we access members, now we will access names

Accessing : Names
int i;
 
for(i=0; i<3; i++)
{
printf("\nName of student%d is %s",  i, s[i],name);
} 

Output:

Name of student1 is Roy
Name of student2 is Jason
Name of student3 is Tom

now we will access div

Accessing : Div
int i;
 
for(i=0; i<3; i++)
{
printf("\nstudent%d studies in  division %c",  i, s[i],div);
} 

Output:

student1 studies in division A
student2 studies in division B
student3 studies in division A
  • This is how we access the member of structure array.
  • Now we will take structure array as input from user.

we will see program of taking members of structure as Input.

Taking Input:
void main()
{
int i;
//declaration of student array.
struct student s[3];

for(i=0; i<3 ;i++)
{
    printf("\nEnter name of student: ");
    gets(s[i].name);//taking name of student
    printf("\nEnter the student roll no:");
    scanf("%d",&s[i].rollno);//taking student rollno
    printf("\nstudent division:");
    scanf("%c",s[i].div);//taking student division
}
}

Output:

Enter name of student:Roy
Enter the student roll no:1
student division:A

Enter name of student:Jason
Enter the student roll no:2
student division:B

Enter name of student:Tom
Enter the student roll no:3
student division:A

Printing the student data:
int i;
for(i=0; i<3 ;i++)
{
printf("\nstudent roll no and Name is  %d and %s and studies in %c division",s[i].rollno,s[i].name,s[i].div);
   
}

Output:

student roll no and Name is 1 and Roy and studies in A division
student roll no and Name is 2 and Jason and studies in B division
student roll no and Name is 3 and Tom and studies in A division
  • This how we can take members of structres as input from User.
  • This how we can work with array of structure In C.







Further Concept:


People Also Searched: