C Language



Admission Enquiry Form

  

Passing Structure to Function

Passing Structure to Function in C

We can also pass structure to a function. In this concept function we take structure as an argument.

Example:

#include<stdio.h>
struct student
{
int rollno;
float fee;
char name[10];
}st;

void main()
{
//struct student st;
void show(struct student); //prototype of function
printf("Enter rollno ");
scanf("%d",&st.rollno);
printf("Enter fee ");
scanf("%f",&st.fee);
printf("Enter name ");
scanf("%s",&st.name);
show(st);  //calling of function
}
void show(struct student s)     //body or definition of function
{
printf("\nRollno is %d",s.rollno);
printf("\nFee is %f",s.fee);
printf("\nName is %s",s.name);
}

Output

Enter rollno 20
Enter fee 5000
Enter name sunil

Rollno is 20
Fee is 5000.000000
Name is sunil