Array as member to Structure
ARRAY AS A MEMBER TO STRUCTURE
An array can be declared as a member to structure as follows:
#include <stdio.h>
// Define a structure named 'Student' with an array member
struct Student {
char name[50];
int age;
float marks[5]; // Array member i.e marks of 5 subjects
};
Example :
C program to find the sum of marks of 5 subject of a Student. Creat a Structure to display name, age and mark of 5 subjects of a student.
#include<stdio.h>
#include<conio.h>
struct student
{
char name[50];
int age;
int roll;
float marks[5]; // array member
};
int main()
{
struct student s;
int i;
printf("Enter name : ");
gets(s.name);
printf("Enter roll: ");
scanf("%d",&s.roll);
printf("Enter age: ");
scanf("%d",&s.age);
for(i=0;i<5;i++)
{
printf("Enter marks:");
scanf("%f",&s.marks[i]);
sum=sum+s.marks[i];
}
printf("Name=%s\nRoll=%d\nAge=%d\nSum=%d",s.name,s.roll,s.age,sum);
return 0;
}