Structure type arguments in C

Preview
You must first complete Introduction to Structure in C before viewing this Lesson

Structure type arguments in C 

We can pass structure type variables as arguments to functions. There are three methods in which structure type argument can be passed to a function.

  • Passing each element of structure variable independently.
  • Passing structure type argument by value(Call by Value)
  • Passing structure type argument by reference(Call by Reference)



Passing each element of structure variable independently

In this method, each individual element of a structure type variable is passed as actual argument to a function.

The actual arguments are treated like ordinary variables and they are transferred to formal arguments on one to one basis. Data types of each individual actual argument must be same as the formal argument.

This method is the most commonly used method to pass arguments to a function but this method becomes unmanageable and inefficient if structure contains large number of elements.

Program to demonstrate use of passing each element of structure variable independently.
#include<stdio.h>
struct student
{
int rollno;
float fees;
}; 

void show(int r,float f)
{
printf(“\n%d,%f”,r,f);
}
int main()
{
struct student st={131,550.50};
clrscr();
show(st.rollno,st.fees);
return(0);
}

Output
131,550.500000



Passing structure type argument by value (Call by Value)

In this method, structure type variable is passed as an actual argument to the function and copy of structure variable is passed to the formal argument of the called function.

Data type of actual argument must be same as the formal argument that is both the actual argument as well as formal argument must be of same structure type.

Changes made to the formal arguments are not reflected back to the actual arguments of the function.

Program to demonstrate use of passing structure type argument by value(Call by Value).
#include<stdio.h>
struct data
{
int a,b;
};
void show(struct data d)
{
++d.a;
–d.b;
}
int main()
{struct data d1={34,55};
printf(“\nd1.a=%d”,d1.a);
printf(“\nd1.b=%d”,d1.b);
show(d1);
printf(“\nd1.a=%d”,d1.a);
printf(“\nd1.b=%d”,d1.b);
return(0);
}
Output
d1.a=34
d1.b=55
d1.a=34
d1.b=55
Description
In the above program, structure variable d1 is passed as an actual argument to the function named show() and it is copied to the formal argument d. Any changes made to d have no effect on the actual argument d1.



Passing structure type argument by reference (Call by Reference)

In this method, structure type variable is passed as an actual argument function and address of structure variable is stored in the formal argument of the called function.

The data type of actual argument must be same as the formal argument that is both the actual argument as well as formal argument must be of same structure type.

Any changes made to the formal arguments are reflected back to the actual arguments of the function.

Program to demonstrate use of passing structure type argument by reference (Call by reference).
#include<stdio.h>
struct data
{
int a,b;
};

 

void show(struct data *d)
{
++(*d).a;
– -d->b;
}
int main()
{struct data d1={34,55};
printf(“\nd1.a=%d”,d1.a);
printf(“\nd1.b=%d”,d1.b);
show(&d1);
printf(“\nd1.a=%d”,d1.a);
printf(“\nd1.b=%d”,d1.b);
return(0);
}

Output
d1.a=34
d1.b=55
d1.a=35
d1.b=54
Description
In the above program, structure variable d1 is passed as an actual argument to the function named show() and it is passed to the formal argument d. Any changes made to d are reflected on the values of actual argument d1.



Best Books of C





Lesson tags: passing structure type arguments by reference, passing structure type arguments by value, passing structure type arguments to function
Back to: C Programming Language