For structures to be fully useful, we must have a mechanism to pass them to functions and return them.
STRUCTURES
AND FUNCTIONS
For
structures to be fully useful, we must have a mechanism to pass them to
functions and return them. A function may access the members of a structure in
three ways as shown in Figure 8.4.
To
pass any individual member of the structure to a function we must use the
direct selection operator to refer to the individual members for the actual
parameters. The called program does not know if the two variables are ordinary
variables or structure members. Look at the following code that illustrates
this concept.
#include <stdio.h>
typedef struct
{
int x;
int y;
}POINT;
void display (int, int);
int main()
{
POINT p1 = {2, 3};
display (p1.x, p1.y);
return 0;
}
void display (int a, int b)
printf("The coordinates of the
point are: %d %d", a, b);
Output
The coordinates of the point are: 2
3
Just
like any other variable, we can pass an entire structure as a function
argument. When a structure is passed as an argument, it is passed using the
call by value method, i.e., a copy of each member of the structure is made.
This is a very inefficient method especially when the structure is very big or
the function is called frequently. In such a situation passing and working with
pointers may be more efficient.
The
general syntax for passing a structure to a function and returning a structure
can be given as de SMIT
struct struct_name func_name
(struct struct_ name struct_var);
This
syntax can vary as per need. For example, in some situations we may want a
function to receive a structure but return a void or value of some other data
type. The following code passes a structure to the function using the
call-by-value method.
#include <stdio.h>
typedef struct
{
int x;
int y;
}POINT;
void display (POINT);
main ()
{
POINT p1 = {2, 3};
display (p1);
return 0;
}
void display (POINT p)
{
printf("%d %d", p.x,
p.y);
}
8.
Write a program to read, display, add, and subtract two distances. Distance must be defined using kms and metres.
#include <stdio.h>
#include <conio.h>
typedef struct distance
{
int kms;
int metres;
}DISTANCE;
DISTANCE add_distance (DISTANCE,
DISTANCE);
DISTANCE subtract_distance
(DISTANCE,
DISTANCE);
DISTANCE d1, d2, d3, d4;
int main()
{
int option;
clrscr();
do
printf("\n ***** MAIN MENU *****");
printf("\n 1. Read the
distances ");
printf("\n 2. Display the
distances");
printf("\n 3. Add the
distances");
printf("\n 4. Subtract the
distances");
printf("\n 5. EXIT");
printf("\n Enter your option:
");
scanf("%d", &option);
switch (option)
{
case 1:
printf("\n Enter the first
distance in kms and metres: ");
scanf("%d %d",
&d1.kms, &d1. metres);
printf("\n Enter the second
distance in kms and metres: ");
scanf("%d %d", &d2.
kms, &d2. metres);
break;
case 2:
printf("\n The first distance
is: %d kms %d metres", d1.kms, d1. metres);
printf("\n The second distance
is: %d kms %d metres", d2.kms, d2. metres);
break;
case 3:
d3 = add_distance (d1, d2);
printf("\n The sum of two
distances is: %d kms %d metres", d3.kms, d3.metres);
break;
case 4:
d4 = subtract distance (d1, d2);
printf("\n The difference
between two distances is: %d kms %d metres", d4. kms, d4.metres);
break;
}
}while (option != 5);
getch();
return 0;
}
DISTANCE add_distance (DISTANCE d1,
DISTANCE d2)
{
DISTANCE sum;
sum.metres = d1.metres + d2.
metres;
sum.kms d1.kms + d2.kms;
if (sum.metres >= 1000)
{
sum.metres = sum.metres%1000;
sum.kms += 1;
}
return sum;
}
DISTANCE subtract_distance
(DISTANCE d1, DISTANCE d2)
{
DISTANCE sub;
if (d1.kms > d2.kms)
{
sub.metres = d1.metres - d2.
metres;
sub.kms = d1.kms d2. kms;
}
else
{
sub.metres = d2.metres – d1.
metres;
sub.kms = d2.kms - d1.kms;
}
if (sub.metres < 0)
{
sub.kms = sub.kms - 1;
sub.metres = sub.metres + 1000;
}
return sub;
}
Output
***** MAIN MENU *****
1. Read the distances
2. Display the distances
3. Add the distances
4. Subtract the distances
5. EXIT
Enter your option: 1
Enter the first distance in kms and
metres: 5 300
Enter the second distance in kms
and metres: 3 400
***** MAIN MENU *****
1. Read the distances
2. Display the distances
3. Add the distances
4. Subtract the distances
5. EXIT
Enter your option: 3
The sum of two distances is: 8 kms
700 metres
9.
Write a program to read, display, add, and subtract two time variables defined
using hours, minutes, and seconds.
#include <stdio.h>
#include <conio.h>
typedef struct
{
int hr;
int min;
int sec;
}TIME;
TIME t1, t2, t3, t4;
TIME subtract_time (TIME, TIME);
TIME add_time (TIME, TIME);
int main()
{
int option;
clrscr();
do
{
printf("\n **** MAIN MENU
****");
printf("\n 1. Read time
");
printf("\n 2. Display
time");
printf("\n 3. Add");
printf("\n 4. Subtract");
printf("\n 5. EXIT");
printf("\n Enter your option:
");
scanf("%d", &option);
switch (option)
{
case 1:
printf("\n Enter the first
time in hrs, mins, and secs: ");
scanf("%d %d %d",
&t1.hr, &t1.min, &t1.sec);
printf("\n Enter the second
time in hrs, mins, and secs: ");
scanf("%d %d %d",
&t2.hr, &t2.min, &t2.sec);
break;
case 2:
printf("\n The first time is:
%d hr %d min %d sec", t1.hr, t1.min, t1.sec);
printf("\n The second time is:
%d hr %d min %d sec", t2.hr, t2.min, t2.sec);
break;
case 3:
t3 = add time (t1, t2);
printf("\n The sum of the two
time values is: %d hr %d min %d sec", t3.hr, t3.min, t3.sec);
break;
case 4:
t4 = subtract_time (t1, t2);
printf("\n The difference in
time is: %d hr %d min %d sec", t4.hr, t4.min, t4.sec);
break;
}
} while (option != 5);
getch();
return 0;
}
TIME add_time (TIME t1, TIME t2)
{
TIME sum;
sum.sec = t1.sec + t2.sec;
while (sum.sec >= 60)
{
sum.min =60;
sum.hr++;
}
sum.min = t1.min + t2.min;
while(sum.min >= 60)
{
sum.sec -=60;
sum.min++;
}
sum.hr = t1.hr + t2.hr;
return sum;
}
TIME subtract_time (TIME t1, TIME
t2)
{
TIME sub;
if (tl.hr > t2.hr)
{
if (tl.sec < t2.sec)
{
t1.sec += 60;
t1.min--;
}
sub.sec = tl.sec - t2.sec;
if (t1.min < t2.min)
{
t1.min += 60;
t1.hr--;
}
sub.min = t1.min - t2.min;
sub.hr = t1.hr - t2.hr;
}
else
{
if (t2.sec < tl.sec)
{
t2.sec += 60;
t2.min--;
}
sub.sec = t2.sec -t1.sec;
if (t2.min < t1.min)
{
t2.min += 60;
t2.hr--;
}
sub.min = t2.min - t1.min;
sub.hr = t2.hr - t1.hr;
}
return sub;
}
Output
**** MAIN MENU ****
1. Read time
2. Display time
3. Add
4. Subtract
5. EXIT
Enter your option: 1
Enter the first time in hrs, mins,
and secs:
2 30 20
Enter the second time in hrs, mins,
and secs:
3 20 30
**** MAIN MENU ****
1. Read time
2. Display time
3. Add
4. Subtract
5. EXIT
Enter your option: 3
The sum of the two time values is:
5 hr 50
min 50 sec
Let
us summarize some points that must be considered while passing a structure to a
function.
•
If the called function is returning a copy of the entire structure then its
return type must be declared as struct followed by the structure name.
•
The structure variable used as parameter in the function declaration must be
the same as that of the actual argument in the called function (and that should
be the name of the struct type).
•
When a function returns a structure then in the calling function the returned
structure must be assigned to a structure variable of the same type.
Programming Tip:
Using
pointers to pass a structure to a function is more efficient than using the
call-by-value method.
Passing
large structures to functions using the call-by- value method is very
inefficient. Therefore, it is preferred to pass structures through pointers. It
is possible to create a pointer to almost any type in C, including user-defined
types. It is extremely common to create pointers to structures. As in other
cases, a pointer to a structure is never itself a structure, but merely a
variable that holds the address of a structure. The syntax to declare a pointer
to a structure can be given as
struct struct name
{
data_type member_name1;
data_type member_name2;
data_type member_name3;
……………………………..
}*ptr;
OR
struct struct_name *ptr;
For
our student structure can declare a pointer we variable by writing
struct student *ptr_stud, stud;
The
next thing to do is to assign the address of stud to the pointer using the
address operator (&) as we would do in case of any other pointer. So to
assign the address, we will write
Ptr_stud
= &stud;
To
access the members of the structure, one way is to write
/* get the structure, then select a
member */ (*ptr_stud). roll_no;
Programming Tip:
The
selection operator (->) is a single token, so do not place any white space
between them.
Since
parentheses have a higher precedence than *, writing this statement would work
well. But this statement is not easy for a beginner to work with. So C
introduces a new operator to do the same task. This operator is known as the
pointing-to operator (->). Here it is being used:
/* the roll_no in the structure
ptr_stud points to */
ptr_stud -> roll_no = 01;
This
statement is far easier than its alternative.
10.
Write a program, using a pointer to a structure to initialize the members of
the structure.
#include <stdio.h>
#include <conio.h>
struct student
{
int r no;
char name [20];
char course [20];
int fees;
};
int main()
{
struct student studl, stud2,
*ptr_stud1,
*ptr_stud2;
clrscr();
ptr_studl = &stud1;
ptr_stud2 = &stud2;
ptr_stud1-> r_no = 01;
strcpy (ptr_stud1 -> name,
"Rahul ");
strcpy (ptr_stud1-> course,
"BCA");
ptr_stud1 -> fees = 45000;
printf("\n Enter the details
of the second student: ");
printf("\n Enter the Roll
Number = ");
scanf("%d",
&ptr_stud2 -> r_no);
printf("\n Enter the Name
=");
gets(ptr_stud2 ->name);
printf("\n Enter the Course
=");
gets (ptr_stud2 -> course);
printf("\n Enter the Fees =
" );
scanf("%d",
&ptr_stud2 -> fees);
printf("\n DETAILS OF FIRST
STUDENT");
printf("\n ROLL NUMBER =
%d", ptr_studl->r_no);
printf("\n NAME = %s",
ptr_stud1 ->name);
printf("\n COURSE = %s",
ptr_stud1->course);
printf("\n FEES = %d",
ptr_stud1 -> fees);
printf("\n\n\n\n DETAILS OF
SECOND STUDENT");
printf("\n ROLL NUMBER =
%d", ptr_stud2->r_no);
printf("\n NAME = %s",
ptr_stud2 ->name);
printf("\n COURSE = %s",
ptr_stud2->course);
printf("\n FEES = %d",
ptr_stud2 -> fees);
return 0;
}
Output
Enter the details of the second
student:
Enter the Roll Number = 02
Enter the Name = Aditya
Enter the Course = MCA
Enter the Fees = 60000
DETAILS OF FIRST STUDENT
ROLL NUMBER = 01
NAME = Rahul
NAME = Aditya
COURSE = MCA
FEES = 60000.00
->
roll_n_no = 0;
20
11.
Write a program, using a pointer to a structure, to initialize the members of
the structure using an alternative technique.
#include <stdio.h>
#include <conio.h>
struct student
{
int r_no;
char name [20];
char course [20];
float fees;
};
int main()
{
struct student *ptr_stud1;
struct student stud1 = {01,
"Rahul", "BCA", 45000};
clrscr();
ptr_stud1 = &stud1;
printf("\n DETAILS OF
STUDENT");
printf("\n ROLL NUMBER =
%d", ptr_studl->r_no);
printf("\n NAME = %s",
ptr_stud1 -> name);
printf("\n COURSE = %s",
ptr_studl-> course);
printf("\n FEES = %f",
ptr_stud1 -> fees);
return 0;
}
Output
DETAILS OF STUDENT
ROLL NUMBER = 01
NAME = Rahul
COURSE = BCA
FEES = 45000.00
12.
Write a program, using an array of pointers to a structure, to read and display
the data of a student.
#include <stdio.h>
#include <conio.h>
#include <alloc.h>
typedef struct student
{
int r_no;
char name [20];
char course [20];
int fees;
};
struct student *ptr [10];
int main()
{
int i;
for (i=0;i<2; i++)
{
ptr[i] = (struct student
*)malloc(size of (struct student));
printf("\n Enter the data for
student %d ",i+1);
printf("\n ROLL NO.: ");
scanf("%d",
&ptr[i]->r_no);
printf("\n NAME: ");
gets (ptr[i] ->name);
printf("\n COURSE: ");
gets (ptr[i] -> course);
printf("\n FEES: ");
scanf("%d", &ptr[i]
-> fees);
}
printf("\n DETAILS OF
STUDENTS");
for (i=0;i<2; i++)
{
printf("\n ROLL NUMBER =
%d", ptr_stud[i] => r_no);
printf("\n NAME = %s", ptr_stud[i]
->name);
printf("\n COURSE = %s", ptr_stud[i]
-> course);
printf("\n FEES = %d",ptr_stud[i]
-> fees);
}
return 0;
}
Output
Enter the data for student 1
ROLL NO.: 01
NAME: Rahul
COURSE: BCA
FEES: 45000
Enter the data for student 2
ROLL NO.:02
NAME: Priya
COURSE: BCA
FEES: 25000
DETAILS OF STUDENTS
ROLL NUMBER = 1
NAME = Rahul
COURSE = BCA
FEES = 45000
ROLL NUMBER = 2
NAME = Priya
COURSE = BCA
FEES = 25000
13.
Write a program to read, display, add, and subtract two heights. Height should
be given in feet and inches.
#include <stdio.h>
#include <conio.h>
typedef struct
{
int ft;
int inch;
}HEIGHT;
HEIGHT h1, h2, h3;
HEIGHT add_height (HEIGHT *,HEIGHT
*);
HEIGHT subtract_height (HEIGHT*,
HEIGHT *);
int main()
{
int option;
clrscr();
do
{
printf("\n *** MAIN MENU
***" );
printf("\n 1. Read height
");
printf("\n 2. Display height
");
printf("\n 3. Add");
printf("\n 4. Subtract");
printf("\n 5. EXIT");
printf("\n Enter your option:
");
scanf("%d", &option);
switch (option)
{
case 1:
printf("\n Enter the first
height in feet and inches: ");
scanf("%d %d",
&hl.ft, &hl.inch);
printf("\n Enter the second
height in feet and inches: ");
scanf("%d %d", &h2.
ft, &h2.inch);
break;
case 2:
printf("\n The first height
is: %d ft %d inch", hl. ft, hl.inch);
printf("\n The second height
is: %d ft %d inch", h2. ft, h2.inch);
break;
case 3:
h3 = add_height (&h1, &h2);
printf("\n The sum of two
heights is: %d ft %d inch", h3. ft, h3.inch);
break;
case 4:
h3 = subtract_height (&hl,
&h2);
printf("\n The difference of
two heights is: %d ft %d inch", h3.ft, h3.inch);
break; }
} `
} while (option != 5);
getch();
return 0;
}
HEIGHT add_height (HEIGHT *h1,
HEIGHT *h2)
{
HEIGHT sum;
sum.inch = h1-> inch + h2 ->
inch;
while (sum. inch > 12)
{
sum.inch - = 12;
sum.ft++;
}
sum.ft = h1-> ft + h2->ft;
return sum;
}
HEIGHT subtract_height (HEIGHT *h1,
HEIGHT *h2)
{
HEIGHT sub;
if (hl->ft > h2-> ft)
{
if (h1-> inch <h2 -> inch)
{
h1-> inch += 12;
h1 -> ft--;
}
sub.inch = h1-> inch - h2->inch;
sub.ft = h1-> ft - h2 -> ft;
}
else
{
if (h2 -> inch < h1 ->
inch)
{
h2 -> inch += 12;
h2->ft--;
}
sub.inch = h2 -> inch - h1 ->
inch;
sub.ft = h2 -> ft - h1-> ft;
}
return sub;
}
Output
*** MAIN MENU ***
1. Read height
2. Display height
3. Add
4. Subtract
5. EXIT
Enter your option: 1
Enter the first height in feet and
inches: 2 3
Enter the second height in feet and
inches: 4 5
*** MAIN MENU ***
1. Read height
2. Display height
3. Add
4. Subtract
5. EXIT
Enter your option: 3
The sum of two heights is: 6 ft 8
inch
14.
Write a program that passes a pointer to a structure to a function.
#include <stdio.h>
#include <conio.h>
#include <alloc.h>
typedef struct student
{
int r_no;
char name [20];
char course [20];
int fees;
};
void display (struct student *);
int main()
{
struct student *ptr;
ptr = (struct student
*)malloc(sizeof (struct student));
printf("\n Enter the data for
the student ");
printf("\n ROLL NO.: ");
scanf("%d",
&ptr->r_no);
printf("\n NAME: ");
gets (ptr->name);
printf("\n COURSE: ");
gets (ptr-> course);
printf("\n FEES: ");
scanf("%d", &ptr
-> fees);
display (ptr);
getch();
return 0;
}
void display (struct student *ptr)
{
printf("\n DETAILS OF
STUDENT");
printf("\n ROLL NUMBER =
%d", ptr->r_no);
printf("\n NAME = %s",
ptr->name);
printf("\n COURSE = %s",
ptr->course)
printf("\n FEES = %d",
ptr-> fees);
}
Output
Enter the data for the student
ROLL NO.: 01
NAME: Rahul
COURSE: BCA
FEES: 45000
DETAILS OF STUDENT
ROLL NUMBER = 01
NAME = Rahul
COURSE = BCA
FEES = 45000.00
15.
Write a program to illustrate the use of arrays within a structure.
#include <stdio.h>
#include <conio.h>
#include <alloc.h>
typedef struct student
{
char name [20];
int roll_no;
int marks [3];
};
void display (struct student* s)
{
int i;
printf("\n NAME = %s \n ROLL
NO = %d\n", s->name, s->roll_no);
printf("\n MARKS = ");
for (i = 0; i < 3;i++)
printf("%d", s->marks
[i]);
}
int main()
{
struct student *s [2];
int i,j;
clrscr();
for (i=0; i < 2;i++)
{
s[i]=(struct student*)
malloc(sizeof (struct student));
printf("\n\n Enter the name of
student %d: ", i+1);
gets (s[i]->name);
printf("\n Enter the roll
number of student %d: ", i+1);
scanf("%d", &s [i]
-> roll_no);
printf("\n Enter the marks
obtained in three subjects by student %d: ", i+1);
for(j = 0; j < 3;j++)
scanf("%d", &s [i]
-> marks [j]);
}
printf("\n \n\n
*****DETAILS***** " );
for (i=0;i<2; i++)
display (s[i]);
getch();
return 0;
}
Output
Enter the name of student 1:
Goransh
Enter the roll number of student 1:
01
Enter the marks obt obtained in
three e subjects by student 1: 99 100 99
Enter the name of student 2:
Pranjal
Enter the roll number of student 2:
02
Enter the marks obtained in three
subjects by student 2: 90 100 89
*****DETAILS*****
NAME = Goransh
ROLL NO = 01
MARKS = 99 100 99
NAME = Pranjal
ROLL NO = 02
MARKS = 90 100 89
Programming in C: Unit IV: Structures and Union : Tag: : with Example C Programs - Structure and Functions
Programming in C
CS3251 2nd Semester CSE Dept 2021 | Regulation | 2nd Semester CSE Dept 2021 Regulation