STRUCTURES
AND FUNCTIONS (or) PASSING STRUCTRES TO FUNCTIONS
A structure can also be passed as an argument to a
function in three ways. Those are:
Case 1: Passing individual member of the
structure as an argument
Case 2: Passing entire structure as an
argument
Case 3: Passing address of the structure as
an argument
Case 1:
- When an individual member of the structure is passed as an argument to a function, a copy of member value is passed from calling function to the called function.
- Here, Member value is copied into the formal argument of the called function.
- Then if we made any changes inside the function with the received value, those changes are not recognized by the outside function. Since, entire changes are effects only on the formal arguments.
/* TO PASS INDIVIDUAL MEMBER OF THE STRUCTURE AS AN ARGUMENT */
#include<stdio.h>
#include<conio.h>
struct
cricket
{
char name[15];
int matches;
float avg;
}s={"sachin",200,76.78};
void
change(int);
main()
{
clrscr();
printf("\nBefore
Passing:");
printf("\n%s\t%d\t%.2f",s.name,s.matches,s.avg);
change(s.matches);
printf("\nAfter
Passing:");
printf("\n%s\t%d\t%.2f",s.name,s.matches,s.avg);
}
void
change(int x)
{
x=210;
}
Case 2:
- When entire structure is passed as an argument to a function from calling function to the called function, the receiving argument at the called function must be a structure of the same type.
- Here, a copy of the entire structure is copied into the formal structure of the called function.
- Then if we made any changes inside the function with the received argument, those changes are not recognized by the outside function. Since, entire changes effects only on the formal structure.
/*
PROGRAM TO PASS ENTIRE STRUCTURE AS AN ARGUMENT */
#include<stdio.h>
#include<conio.h>
#include<string.h>
struct
cricket
{
char name[15];
int matches;
float avg;
}s={"sachin",210,76.78};
void
change(struct cricket);
main()
{
clrscr();
printf("\nBefore
Passing:");
printf("\n%s\t%d\t%.2f",s.name,s.matches,s.avg);
change(s);
printf("\nAfter
Passing:");
printf("\n%s\t%d\t%.2f",s.name,s.matches,s.avg);
}
void
change(struct cricket x)
{
strcpy(x.name,"sehwag");
x.matches=250;
x.avg=50.23;
}
Case 3:
- When address of the structure is passed as an argument to a function from calling function to the called function, the receiving argument at the called function must be a structure pointer variable of the same structure type.
- With this the pointer variable indirectly points to the original structure.
- Then if made any changes inside the function on structure, those changes are recognized by outside function. So, that all changes are effects on the original structure.
/* PROGRAM TO PASS ADDRESS OF THE STRUCTURE AS AN ARGUMENT */
#include<stdio.h>
#include<conio.h>
#include<string.h>
struct
cricket
{
char name[15];
int matches;
float avg;
}s={"sachin",210,76.78};
void
change(struct cricket *);
main()
{
clrscr();
printf("\nBefore
Passing:");
printf("\n%s\t%d\t%.2f",s.name,s.matches,s.avg);
change(&s);
printf("\nAfter
Passing:");
printf("\n%s\t%d\t%.2f",s.name,s.matches,s.avg);
}
void
change(struct cricket *k)
{
strcpy(k→name,"sehwag");
k→matches=250;
k→avg=50.23;
}
No comments:
Post a Comment