Array of structure type | array within structure

Preview
You must first complete Introduction to Structure in C before viewing this Lesson

We can use arrays with a structure in many ways:

1. Array of Structure type

Just like array of basic data types, we can also declare an array of structure type. The main feature of structure type array is that all the members of structure are associated with each and every element of structure.

The syntax for declaring an array of structure type is:

struct <tag_name> Arr_name[Size];

Here, struct is the keyword which specifies that a structure type array is being declared.

tag_name refers to the name of an existing structure of which we want to declare an array.

Here, tag_name is the name of structure of which we want to declare variables.

Arr_name  specifies the name of array as defined by the programmer. The name of array must follow all the rules of a valid identifier of C language.

[ ] Square brackets are known as subscript, they are used to declare Size of array within them.

Size specifies the maximum number of values which can be stored in an array.

Example:

struct student s[10];

In this example, array with name s of structure student has been declared with size 10.


Program to demonstrate use of structure type array.
#include<string.h>
#include<stdio.h>
struct student
{
int rollno;
char name[20];
};
int main()
{
struct student s[13];
int i,n;
printf(“\nHow many records:=”);
scanf(“%d”,&n);
for(i=0;i<n;i++)
{
printf(“\nEnter rollno of student %d =”,(i+1));
scanf(“%d”,&s[i].rollno);
printf(“\nEnter name of student=”);
fflush(stdin);
gets(s[i].name);
}
for(i=0;i<n;i++)
{
printf(“\nRollno of student %d = %d”,(i+1),s[i].rollno);
printf(“\nName of student = %s”,s[i].name);
}
return(0);
}
Output
How many records:=2
Enter rollno of student 1 =1
Enter name of student=amit
Enter rollno of student 2 =2
Enter name of student=sumit
 
Rollno of student 1 = 1
Name of student = amit
Rollno of student 2 = 2
Name of student = sumit



2. Array within a Structure

Just as normal variables, we can also take an array within the structure body. We can refer to array type members of structure just as we access normal members of a structure.

The syntax for declaring array within a structure is

struct <tag_name>

{

:

:

< Data_Type> Arr_name[Size];

};

 


Program to demonstrate use of array within a structure.
#include<string.h>
#include<stdio.h>
struct student
{
int rollno;
float marks[3];
};
int main()
{
struct student s;
printf(“\nEnter rollno of student=”);
scanf(“%d”,&s.rollno);
printf(“\nEnter marks in three subjects=”);
scanf(“%f%f%f”,&s.marks[0],&s.marks[1],&s.marks[2]);printf(“\nRollno of student=%d”,s.rollno);
printf(“\nMarks in subject1=%f”,s.marks[0]);
printf(“\nMarks in subject2=%f”,s.marks[1]);
printf(“\nMarks in subject2=%f”,s.marks[2]);
return(0);
}
Output
Enter rollno of student=1
Enter marks in three subjects=67
76
87
Rollno of student=1
Marks in subject1=67.000000
Marks in subject2=76.000000
Marks in subject2=87.000000
Description
In the above program, an array named marks has been declared inside the structure student. This array can contain three elements named as marks[0], marks[1] and marks[2].



Best Books of C





Lesson tags: array of structure type in c, array within a structure in c, program of array of structure, program of array within a structure, read structure array in c, structure array in c
Back to: C Programming Language