Structures
Structures
This is unlike the array, in which all the variables must be the same type.
The data items in a structure are called the members of the structure.
e.g.
s1. rollno;
s1. age;
s1. name;
struct stud
{ int rollno;
int age;
}s1={20, 21};
main ( )
{
struct stud s2={21, 21};
…
…
3. Third one uses a tag name and
}
defined outside the function.
struct class1
{
int rollno;
char name[20];
float marks;
};
void main()
{
class1 s1={111, “Joe”, 72.50};
class1 s2={222, “Rishi”, 67.00};
class1 s3;
06/04/2025 Dept of I&CT 12
Assigning and Comparing structure variables :
example
s3=s2; // assignment : s2 to s3
//comparison
if((s3. rollno ==s2. rollno)&&(s3.marks ==
s2.marks))
printf“Student3 and student2 are same\n”);
else
printf“Student3 and student2 are NOT same\
n”);
}
if (s1.rollno == 111)
s1.marks += 10.0;
s1.rollno ++;
++ s1.rollno; //applicable to numeric type members
The precedence of the member operator is higher than all arithmetic and relational
operators and therefore no parentheses are required.
Using this structure write a program to read the information for 3 persons from the keyboard
and print all the details of person having highest salary.
student[1].subject3 45
student[1].subject2
student[2].subject1 46
refer to marks obtained in the
student[2].subject2 42
second subject by the second
student[2].subject3 43
student.
student[1].subject[2];
Refer to the marks obtained in the third subject by the second
student.
for(i=0;i<=2;i++) {
for(j=0;j<=2;j++)
student[i].total+=student[i].sub[j]; //students total
}