Arrays within Structure: C allows arrays as structure members. Single or multidimensional arrays of type float ,int or char can be used as the structure members.
We can declare an array to store multiple values inside structure. Inside the structure we may include as many array members as we require.
The Syntax for declaring array within structure is same as the conventional syntax. The difference in declaring array within structure is that it is declared inside structure.
Array within structure in C Programming.
From the given fig. let us understand how array used within structure in C programming language.
we can use single or multidimensional arrays as a structure members inside the structure of types int,char,float double etc.
Declaration and definition of array within structure is same as that of normal declaration of structure.
in the fig. we have declared structure and array within structure for three students to store theire roll number name,marks obtained in four subjects, total marks and average marks.
The declaration of array within structure is same as given in figure.
After declaraing the array within structure, we have to create the variable of structure to access the structure members.
The variable of array is created using the following method.
struct student s[3];
With the help of structure variable we can use or access the structure members or array memebers of structure.
The Roll Number of first Student can be accessed or read with the help of s[0].rollno.
Name accessed with s[0].name
Four subjects marks obtained by first Student is accessed by
s[0].sub[0]
s[0].sub[1]
s[0].sub[2]
s[0].sub[2]
Simillarly for others is the same with using respective index positions.
Simillarly to accesse others structure member the process is same as with using respective index positions.
To Access the marks obtained by second student in third subject use s[1].sub[2],
simillarly marks obtained by third student in fourth subject use s[2].sub[3].
Let us undestand how to create, access array of structure and array within structure with the help of program given below.
we have used the nested for loop to achive the same in C program.
Arrays within Structure Example in C language.
The example shows how to access individual elements of structure and array within structure to find the total and average marks.
struct student
{
int rollno;
char name[15];
int sub[4];
int tot;
float avg;
};
int main()
{
int i;
float avg=0.0;
struct student s[3];
printf(“\n Enter student Information”);
for(i=0;i<3;i++)
{
printf(“\n Enter %d Student information”,i);
printf(“\n Enter %d Student roll number\n”);
scanf(“%d”,&s[i].rollno);
printf(“\n Enter %d student Name\n”);
scanf(“%s”,s[i].name);
for(int j=0;j<4;j++)
{
printf(“\n Enter %d subject marks\n”,i+1);
scanf(“%d”,&s[i].mark[j]);
s[i].tot+=s[i].mark[j];
}
s[i].avg=s[i].tot/4.0;
}
printf(“\n Student Information\n”);
for(i=0;i<3;i++)
{
printf(“\n %d Student information”,i);
printf(“\n RollNo=%d”,s[i].rollno);
printf(“\n Name=%s”,s[i].name);
printf(:\n Subject Marks\n”);
for(int j=0;j<4;j++)
{
printf(“%d ”,s[i].mark[j]);
}
printf(“\n toal=%d”,s[i].tot);
printf(“\n Average=%.2f”,s[i].avg);
}
return(0);
}
Output:
Enter student Information
Enter 1 Student information
Enter 1 Student roll number
101
Enter 1 student Name
Ajay
Enter 1 subject Mark
50
Enter 2 subject Mark
50
Enter 3 subject Mark
50
Enter 4 subject Mark
50
.
.
.
Enter 3 Student roll number
103
Enter 3 student Name
Raj
Enter 1 subject Mark
50
Enter 2 subject Mark
50
Enter 3 subject Mark
50
Enter 4 subject Mark
50
Student Information
1 Student information
RollNo=101
Name=Ajay
Subject Marks
50
50
50
50
Total=200
Average=50.00
.
.
3 Student information
RollNo=103
Name=Raj
Subject Marks
50
50
50
50
Total=200
Average=50.00
Previous Topic:-->> Array of Structure || Next topic:-->>Structure within Structure.
Other Topics:
Variables and Identifiers
Relational Operators
if-else statements
Switch case
While Loop
Infinite while Loops
C FOR Loop
Infinite for Loops
Continue in Loops
One Dimensional Array
Two Dimensional Arrays
Read and Display 2D Arrays
Types of functions
Passing Array To Functions
Nesting of Function
Array vs Structure
Array of Structure
Structures and Functions
Structures Within Structures
Use Of Pointers In C
File Handling In C
Loops FAQ
Arrays FAQ
count vowels in a file
Function FAQ
Conditional Statements Assignments
For Loops Assignments
Arrays Assignments
Function Assignments
Structure Assignments
Pointers Assignments
Files Assignments
Storage classes Assignments
Binary Files
count words,lines in a file
Copy files
Update File
Continue in Loops
break in Loops
Difference Between While and Do while
difference while do..while & for
malloc
calloc
Storage Classes
Operators MCQ
Conditional Statements MCQ
Loops MCQ
Arrays MCQ
Function MCQ
Structure MCQ
Pointers MCQ
Files MCQ
Storage classes MCQ