Structures
Structures
ARRAYS OF STRUCTURES
Arrays of structures means that the structure variable would be an array of
objects, each of which contains the member elements declared within the structure
construct.
Now, to assign values to the ith student of the class, we can write as
stud[i].r_no = 09;
stud[i].name =
"RASHI"; stud[i].course
= "MCA"; stud[i].fees =
60000;
Write a program to print the tickets of the boarders of a boat using array
of structures with initialization in the program.
#include <stdio.h>
int main()
{
int i;
struct boat ticket[4][3]={{“Vikram”,1,15.50},{“Krishna”,2,15.50},
{“Ramu”,3,25.50},{“Gouri”,4, 25.50 } };
printf(“\n passenger Ticket num. Fare”);
for(i=0;i<=3;i++)
printf(“\n %s %d %f ”, ticket[i].name,ticket[i].seatnum,ticket[i].fare);
return 0;
}
Output:
Passenger Ticket num. Fare
Vikram 1 15.500000
Krishna 2 15.500000
Ramu 3 25.500000
Gouri 4 25.500000
#include<stdio.h>
struct emp
{
int empno ;
char name[10] ;
int bpay, allow, ded, npay ;
} e[10] ;
void main()
{
int i, n ;
Write a program to read and display the information of all the students
in a class. Then edit the details of the ith student and redisplay the
entire information.
#include
<stdio.h>
#include
<string.h>
struct student
{
int roll_no;
char
name[80];
int fees;
char DOB[80];
};
int main()
{
struct student
stud[50]; int n, i,
num, new_rollno;
int new_fees;
char new_DOB[80],
new_name[80]; clrscr(); //
clear screen
printf("\n Enter the number of
students : "); scanf("%d", &n);
for(i=0;i<n;i++)
{
printf("\n Enter the roll
number : "); scanf("%d",
&stud[i].roll_no);
for(i=0;i<n;i++)
{
UNIT-II EC8393-FUNDAMENTALS OF DATA STRUCTURES IN C
ROHINI COLLEGE OF ENGINEERING AND TECHNOLOGY
printf("\n ********DETAILS OF STUDENT
%d*******", i+1); printf("\n ROLL No. = %d",
stud[i].roll_no);
printf("\n NAME = %s",
stud[i].name); printf("\n FEES
= %d", stud[i].fees); printf("\n
DOB = %s", stud[i].DOB);
}
stud[num].roll_no = new_rollno;
strcpy(stud[num].name, new_name);
stud[num].fees = new_fees;
strcpy (stud[num].DOB, new_DOB);
for(i=0;i<n;i++)
{
printf("\n ********DETAILS OF STUDENT
%d*******", i+1); printf("\n ROLL No. = %d",
stud[i].roll_no);
printf("\n NAME = %s",
stud[i].name); printf("\n FEES
= %d", stud[i].fees); printf("\n
DOB = %s", stud[i].DOB);
}
getch();
return 0; Output
} Enter the number of students : 2 Enter the roll number : 1
Enter the name : kirti Enter the fees : 5678 Enter the DOB : 9-
9- 99
Enter the roll number : 2 Enter the name : kangana Enter the
fees : 5678 Enter the DOB : 27- 8- 99
********DETAILS OF STUDENT 1*******ROLL No. = 1
NAME = kirtiFEES = 5678
DOB = 9- 9- 99
********DETAILS OF STUDENT 2*******ROLL No. = 2
NAME = kanganaFEES = 5678
DOB = 27- 8 -99
Enter the student number whose record has to be edited : 2Enter the new roll number : 2
Enter the new name : kangana khullarEnter the new fees : 7000
Enter the new DOB : 27- 8 -92
#include
<stdio.h>
typedef struct
{
int
x;
int
y;
}POINT;
int main()
{
POINT p1 = {2, 3}; Output
display(p1.x, p1.y); // function call The coordinates of the point are: 2 3
return 0;
}
#include<stdio.h>
typedef struct
{
int x;
int y;
}POINT;
void display(POINT);
int main()
{
POINT p1 = {2, 3};
display(p1);
return 0;
}
void display(POINT p)
{
UNIT-II EC8393-FUNDAMENTALS OF DATA STRUCTURES IN C
ROHINI COLLEGE OF ENGINEERING AND TECHNOLOGY
printf("The coordinates of the point are: %d %d", p.x, p.y);
}
struct Employee
{
int Id;
char
Name[25]; int
Age;
long Salary;
};
void main()
{
struct Employee Emp =
{1,"Kumar",29,45000}; Display(Emp);
Output
:
Employee Id : 1
Employee Name :
Kumar Employee Age
UNIT-II EC8393-FUNDAMENTALS OF DATA STRUCTURES IN C
ROHINI COLLEGE OF ENGINEERING AND TECHNOLOGY
: 29 Employee Salary
: 45000
Program
#include<stdio.h>
struct
Employee
{
int Id;
char
Name[25]; int
Age;
long Salary;
};
void Display(struct
Employee*); void main()
{
struct Employee Emp =
{1,"Kumar",29,45000}; Display(&Emp);
Output :
Employee Id:
EmployeeName: Kumar
EmployeeAge: 29
EmployeeSalary : 45000