Function Returning Structure in C
Function Returning Structure in C
As we have learnt in function topic,that a function can return a value may be of int,char or float type.Simillarly, a function can also return a structure with the values of all its data members.
Example:
#include<stdio.h>
struct student
{
int rollno;
float fee;
char name[10];
}st;/*Here st is a global variable,
means main() can also use and
input() can also use */
void main()
{
//struct student st;
struct student input(); //prototype of function
st=input(); //calling of function
printf("\nRollno is %d",st.rollno);
printf("\nFee is %f",st.fee);
printf("\nName is %s",st.name);
}//end of main
struct student input() //body
{
printf("Enter rollno ");
scanf("%d",&st.rollno);
printf("Enter fee ");
scanf("%f",&st.fee);
printf("Enter name ");
scanf("%s",&st.name);
return(st);
}
Output
Enter rollno 10
Enter fee 10000
Enter name sunil
Rollno is 10
Fee is 10000.000000
Name is sunil