C – Arrays in Structure

Arrays in structure: If the structure contains Array type variable, we need to process the data using loops.

#include<stdio.h>
struct Student
{
            int id;
            char name[20];
            int marks[5];
};
 
int main()
{
            struct Student s;
            int i;
            printf(“Enter Student id and name : \n”);
            scanf(“%d%s”, &s.id, s.name);
            printf(“Enter student marks of 5 subjects : \n”);
            for(i=0 ; i<5 ; i++)
            {
                        scanf(“%d”, &s.marks[i]);
            }
            printf(“Student details are : \n”);
            printf(“%d, %s, “, s.id, s.name);
            for(i=0 ; i<5 ; i++)
            {
                        printf(“%d ,”, s.marks[i]);
            }
            return 0;
}

Store more than one student records including their marks:

#include<stdio.h>
struct student
{          
            int id;  
            char name[20];            
            int marks[5];
};
int main()
{          
            struct student a[3];      
            int i,j;   
            printf(“Enter student details\n”);         
            for(i=0;i<3;i++)
            {                      
                        printf(“Enter student id and name\n”);                        
                        scanf(“%d%s”,&a[i].id,a[i].name);                    
                        printf(“Enter marks of student\n”);                 
                        for(j=0;j<5;j++)
                        {
                                    scanf(“%d”,&a[i].marks[j]);
                        }
            }          
            printf(“Student details are \n”);           
            for(i=0 ; i<3 ; i++)
            {                      
                        printf(“%d \t %s \t “,a[i].id,a[i].name);
                        for(j=0;j<5;j++)
                        {
                                    printf(“%d\t”,a[i].marks[j]);      
                        }
                        printf(“\n”);     
            }          
            return 0;
}
Scroll to Top