Structure type arguments in C

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



Structure type arguments in C 

Structure type variables can be passed as arguments to functions. Two methods to pass structure type arguments are:

  • To pass each structure element as argument independently.
  • To pass structure type variable as argument.

To pass each structure element as argument independently

In this case Each individual element of a structure variable is passed as an argument to function.

Program to pass each structure element as argument independently.
#include<stdio.h>
struct emp
{
int empid;
float sal;
};

 

void show(int i,float s)
{
printf(“\n%d,%f”,i,s);
}
int main()
{
struct emp e1={101,5500.00};
show(e1.empid,e1.sal);
return(0);
}

Output
131,550.500000



To pass structure type variable as argument

In this case structure variable is passed as an actual argument to a function

Program to demonstrate passing structure type variable as argument .
#include<stdio.h>
struct demo
{
int x,y;
};
void disp(struct demo d1)
{
++d1.x;
– -d1.y;

 

printf(“\nd1.x=%d”,d1.x);
printf(“\nd1.y=%d”,d1.y);
}
int main()
{

struct demo d={10,15};
show(d1);
return(0);
}

Output
d1.x=11
d1.y=14



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