Professional Documents
Culture Documents
Structure
Example:
struct list
{
int roll;
char name[10];
float marks;
};
Representation of structure:
roll Integer
marks Float
Arrays vs Structures
stu1.roll = 12;
stu1.marks = 85.63;
strcpy(stu1.name, “Ajay”);
stu2.marks = 81.22;
stu2.roll = 10;
or
scanf(“%s”, stu2.name);
scanf(“%d”, &stu1.roll);
scanf(“%f”, &stu2.marks);
Valid declaration.
struct list {
int roll;
char name[10];
float marks;
};
struct list a , b , c;
It is equivalent to :
struct list {
int roll;
char name[10];
float marks;
} a, b, c;
Structure Initialization
struct player
{
int matches, runs;
char name[10];
float average;
};
struct player p1, p2, p3;
p1 = {120,3450,“Raj”,42.1};
p2 = {71,1492,“Ram”,35.81};
void main( )
{
struct student {
int roll;
char name[10];
float marks;
} a, b, c;
printf(“Enter all information of students:” );
scanf(“%d %s %f”, &a.roll, a.name, &a.marks);
scanf(“%d %s %f”, &b.roll, b.name, &b.marks);
scanf(“%d %s %f”, &c.roll, c.name, &c.marks);
Array of structures.
void main( )
{
struct student
{
int roll;
char name[10];
float marks;
} a[10];
int i;
printf(“Enter info. of students: ”);
for(i = 0 ; i < 10 ; i++)
{
scanf(“%d %s %f”, &a.roll[i],
a.name[i], &a.marks[i]);
}
for(i = 0 ; i < 10 ; i++)
{
printf(“\n%d %s %f”,
a.roll[i], a.name[i], a.marks[i]);
}
}
i
a[i].roll a[i].name a[i].marks
0
1
a[4].roll
2
3
4
5
6
7
8 a[6].name
a[9].marks