passing structure to a function by address
Instruction: Program write in notes notebook
For Example: Write a program to demonstrate the use of passing
structure to a function by address
#include<stdio.h>
#include<conio.h>
struct student //Here student is a
structure
{
int rollno;
char sname[20];
char sclass[20];
};
void display(struct student *stu); //Here stu is the pointer variable
of structure, stud is point to s structure variable
// function prototype should be below to the structure
declaration otherwise compiler shows error
void main()
{
struct student s; // Here s a structure variable
printf("\n Enter Roll Number:");
scanf("%d", &s.rollno);
printf("\n Enter Name of Student:
");
scanf("%s", &s.sname);
printf("\n Enter Class of
student:");
scanf("%s",&s.sclass);
display(&s); // Here function call ,(passing structure
variable s as argument)
getch();
}
void display(struct student *stu) //Function definition
{
printf("\n Roll is: %d",stu->rollno);
printf("\n Name of Student is: :
%s",stu->sname);
printf("\n Class of Student is:
%s",stu->sclass);
}
Output :
Comments
Post a Comment