STRUCTURES
AND FUNCTIONS:
Any
user-defined function can be performed on structure values by passing structure
values as arguments to the function. This can be done in three ways:-
(i)
Each member of a student can be passed individually as function arguments
but this method becomes difficult when the structure contains more number of
elements.
Ø
A Program to pass structures as function
arguments
void display(int,float);
void main()
{
struct employee
{
int empid;
float salary;
};
struct employee e1={105,5000.5};
display(e1.empid,e1.salary);
}
void display(int id,float sal)
{
printf(“empid=%d\nsalary=%f”,id,sal);
}
(ii)
A copy of entire structure can be passed from calling
function to the called function. Here, the changes made in called function do
not reflect in the calling function.
Ø
A Program to pass structures as function
arguments
void display(struct employee);
void main()
{
struct employee
{
int empid;
float salary;
};
struct employee e1={105,5000.5};
display(e1);
}
void display(struct employee d)
{
printf(“empid=%d\nsalary=%f”,d.empid,d.salary);
}
(iii)
The address location of the structure can be passed to
the function. Here the changes made in the called function are reflected in the
calling function.
Ø A
Program on passing location of structure to a function:
void display(struct book *);
void main()
{
struct book
{
char name[10];
char author[10];
int pages;
};
struct book b1={“c&ds”,”dennis ”,250};
display(&b1);
}
void display(struct book *b2)
{
printf(“name=%s\nauthor=%s\npages=%d”,(*b2).name,(*b2.author),(*b2.pages));
}
No comments:
Post a Comment