STRUCTURES
06/04/2025 Dept of I&CT 1
Structures
A structure is a collection of one or more variables, possibly of different types,
grouped together under a single name for convenient handling
The variables in a structure can be int, float, and so on.
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.
06/04/2025 Dept of I&CT 2
Structures
The general format of a structure definition is
struct tag_name
{
Data_type member1;
Data_type member2;
…
e.g.
}; struct student
{ int rollno;
int age;
char name[10];
float height;
};
06/04/2025 Dept of I&CT 3
Declaring Structure Variables
There are several equivalent ways to define/declare variables of a particular structure type.
Declare them at the structure definition
struct student
{ int rollno; struct student
{ int rollno;
int age;
char name[10];
int age;
float height; char
name[10];
}s1, s2, s3;/* Define 3 variables */
float
height;
};
Define the variables at some point after the structure definition
struct student s1, s2, s3;
06/04/2025 Dept of I&CT 4
Declaring Structure Variables
Defining a Structure Variable in main() without using struct tag
It can be in the main() as, definition of the structure should be done before ,
struct student
{ int rollno;
student s1; int
age;
Note: char
name[20];
Members of a structure themselves };are not variables.
i.e. rollno alone does not have any value or meaning.
06/04/2025 Dept of I&CT 5
Member or dot operator
The link between member and a structure variable is established using the
member operator ‘.’ which is also known as ‘dot operator’
e.g.
s1. rollno;
s1. age;
s1. name;
06/04/2025 Dept of I&CT 6
Giving values to members
How to assign values to the members of student s1
strcpy([Link], “Ram”);
[Link] = 1335; struct student
[Link] = 18; { int rollno;
int
[Link] = 5.8; age;
char
name[20];
float
height;
}s1;
06/04/2025 Dept of I&CT 7
Structure: Example
struct book { // declaration
char title[20]; struct { // declaration
char title[20];
char author[15];
char author[15];
int pages; OR int pages;
float price; float price;
}; } b1,b2,b3;
void main( ){
struct book b1, b2, b3;
printf(“Input values”);
scanf(“%s%s%d%f”, [Link], [Link], &[Link],
&[Link]);
//output
printf(“%s%s%d%f”, [Link], [Link], [Link],
06/04/2025 [Link]); Dept of I&CT 8
Structure initialization
main ( ){
struct There is one-to-one
{ int rollno; correspondence between the
int age; members and their initializing
}stud={20, 21}; values.
…
}
main ( ){ 1. First one without tag name.
struct stud 2. Second one uses a tag
{ int rollno; name.
int age;
};
struct stud s1={20, 21};
struct stud s2={21, 21};
}
06/04/2025 Dept of I&CT 9
Structure initialization
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.
06/04/2025 Dept of I&CT 10
Assign and Compare structure variables
If student1 and student2 belong to the same structure, then the following operations
are valid:
1. student1 = student2 // Assign student2 to student1
2. if(student1 .rollno == [Link] ) //comparison
return 1 if they are equal,
0 otherwise.
3. if(student1 .rollno != student2 .rollno ) OR
return 1 if they are not equal,
0 otherwise.
The comparison should be done with respect to any member variables.
06/04/2025 Dept of I&CT 11
Assigning and Comparing structure variables :
example
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)&&([Link] ==
[Link]))
printf“Student3 and student2 are same\n”);
else
printf“Student3 and student2 are NOT same\
n”);
}
06/04/2025 Dept of I&CT 13
Operation on Individual members
Individual members are identified using member operator, the dot operator.
A member with the dot operator along with its structure variable can be treated like any other
variable name.
if ([Link] == 111)
[Link] += 10.0;
float sum = [Link] + [Link];
[Link] ++;
++ [Link]; //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.
06/04/2025 Dept of I&CT 14
To be solved …
Define a structure type, struct person that would contain person name, date
of joining (only day(int)) and salary.
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.
06/04/2025 Dept of I&CT 15
Solution:
struct person {
char name[15];
int doj;
float sal;
};
void main(){
struct person p1, p2, p3;
printf("Input values for person 1:- \n“);
scanf(“%s%d
%f”,[Link],&[Link],&[Link];
printf("Input values for person 2:- \n“);
scanf(“%s%d
%f”,[Link],&[Link],&[Link];
printf("Input values for person 2:- \n“);
06/04/2025 Dept of I&CT 16
scanf(“%s%d
Solution:
if ([Link]>[Link] && [Link] >[Link]){
printf"Name: %s\n“,[Link]);
printf("Date of Joining: %d“,[Link]);
printf(“Salary:%f\n”, [Link]);}
else if ([Link]> [Link] && [Link] > [Link]){
printf"Name: %s\n“,[Link]);
printf("Date of Joining: %d“,[Link]);
printf(“Salary:%f\n”, [Link]);}
Else{
printf"Name: %s\n“,[Link]);
printf("Date of Joining: %d“,[Link]);
printf(“Salary:%f\n”, [Link]);}
}
06/04/2025 Dept of I&CT 17
Problems…
Write programs to
1. Create a student record with name, rollno, marks of 3
subjects (m1, m2, m3). Display the details of the students
in ascending order of their average marks.
2. Create an employee record with emp-no, name, age, date-
of-joining (year), and salary. If there is 20% hike on salary
per annum, compute the retirement year of each
employee and the salary at that time. [standard age of
retirement is 55]
06/04/2025 Dept of I&CT 18
Array of structures
We can define single or multidimensional arrays as structure variables.
struct marks
{
int subject1;
int subject2;
int subject3;
} ;
struct marks student[84];
defines an array called student, that consists of 84 elements.
Each element is defined to be the type struct marks.
06/04/2025 Dept of I&CT 19
Array of structures
struct marks
{
int subject1;
int subject2;
int subject3;
} ;
main(){
struct marks student[3]={ {45,47,49},
{43,44,45},
{46,42,43} };
This declares the student as an array of three elements student[0], student[1],
student[2].
06/04/2025 Dept of I&CT 20
Array of structures
The members can be initialized as
student[0].subject1 = 45;
student[0].subject2= 47; Memory
… student[0].subject1 45
… student[0].subject2 47
student[2].subject3= 43; student[0].subject3 49
student[1].subject1 43
Stored in the memory as student[1].subject2 44
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.
06/04/2025 Dept of I&CT 21
Arrays within Structures
We can define single or multidimensional arrays inside a structure.
struct marks
{ int rollno;
float subject[3];
} student[2] ;
The member subject contains 3 elements; subject[0], subject[1] &
subject[2].
student[1].subject[2];
Refer to the marks obtained in the third subject by the second
student.
06/04/2025 Dept of I&CT 22
Arrays within structures : example
struct marks{
int total;
int sub[3];
};
void main(){
marks student[3] ={{0,45,47,49}, {0,43,44,45}, {0,46,42,43} };
int i, j ;
for(i=0;i<=2;i++) {
for(j=0;j<=2;j++)
student[i].total+=student[i].sub[j]; //students total
}
06/04/2025 Dept of I&CT 23
Arrays within structures : example
Printf(“Grand Total of each student.“);
for(i=0;i<=2;i++)
printf("Total of student[“%d"]=
%d“,i,student[i].total;
}
06/04/2025 Dept of I&CT 24
Structures and functions
void read(book []); // prototype
void main() { struct book
int i; {
int isbn;
struct book b1[2]; char
printf(“Enter ISBN, Author name & Price \n“); author[15];
float price;
read(b1); // function call };
function
printf(“The book details entered:\n“); void read(book a[])
for(i=0;i<2;i++){ {
int i;
printf(“%d Book:”, i+1); for(i=0;i<2;i++){
printf(“ISBN: %d“,b1[i].isbn;) scanf(“%d”, &a[i].isbn));
scanf(“%s”, a[i].author);
printf(“Author: %s“, b1[i].author); scanf(“%f”, &a[i].price);
printf("Price: %f“, b1[i].price); }
}
}
}
06/04/2025 Dept of I&CT 25