STRUCTURE AND FUNCTION
1. PASSING A MEMBER OF STRUCTURE TO FUNCTION
Example: WAP to create a structure named Student that has name, age,marks as members. assume appropriate types size of members. Create a function called "display " that prints name of Student. Pass student name as argument to function.
#include<stdio.h>
void display(char n[])
{
printf("NAME \n");
printf("%s",n);
}
int main()
{
struct Student
{
char name[10];
int age;
float marks;
};
struct Student s;
printf("enter the information of student\n");
printf("Enter name\n");
scanf("%s",s.name);
printf("Enter roll\n");
scanf("%d",&s.age);
printf("Enter marks\n");
scanf("%f",&s.marks);
printf("\n THE STUDENT NAME IS \n");
display(s.name);
return 0;
}
2. PASSING ENTIRE STRUCTURE TO FUNCTION
Example: WAP to create a structure named Student that has name, age,marks as members. assume appropriate types size of members . Create a function called "display " that prints each member of structure.Pass whole structure as argument to function
#include<stdio.h>
struct Student
{
char name[10];
int age;
float marks;
}s;
void display(struct Student st)
{
printf("NAME \t\t AGE \t\t MARKS\n");
printf("%s\t\t%d\t\t%f",st.name,st.age,st.marks);
}
int main()
{
printf("enter the information of student\n");
printf("Enter name\n");
scanf("%s",s.name);
printf("Enter roll\n");
scanf("%d",&s.age);
printf("Enter marks\n");
scanf("%f",&s.marks);
printf("\n THE STUDENT INFORMATION IS \n");
display(s);
return 0;
}
3. PASSING STRUCTURE POINTER TO FUNCTION
WAP to create a structure named Employee that has name, id,salary as members. Assume appropriate types size of members . Create a function called "display " that prints each member of structure and another function called "bonus" that increases the salary by Rs 5000.Pass pointer to structure as argument to function bonus.
#include<stdio.h>
struct Employee
{
char name[10];
int id;
float salary;
}e;
void display(struct Employee et)
{
printf("\n THE EMPLOYEE INFORMATION IS \n");
printf("NAME \t\t ID \t\t SALARY\n");
printf("%s\t\t%d\t\t%.2f",et.name,et.id,et.salary);
}
void bonus(struct Employee *eptr)
{
eptr->salary=eptr->salary+5000;
}
int main()
{
printf("enter the information of employee\n");
printf("Enter name\n");
scanf("%s",e.name);
printf("Enter id\n");
scanf("%d",&e.id);
printf("Enter salary\n");
scanf("%f",&e.salary);
bonus(&e);
display(e);
}
4. PASSING ARRAY OF STRUCTURE TO FUNCTION
Example: WAP to create a structure named student that has name, roll, marks as its members. Assume appropriate types and size members . use this structure to read and display records of 5 students. Create two functions: one is to read the information of students and other function to display student information. Pass as array of structure to above functions as their argument.
#include<stdio.h>
struct Student
{
char name[20];
int age;
float marks;
}s1[5];
int i;
void display(struct Student st1[])
{
printf("Name \tRoll \t Marks\t\n");
for(i=0;i<5;i++)
{
printf("%s\t%d\t%f\n",st1[i].name,st1[i].age,st1[i].marks);
}
}
void read(struct Student st[])
{
for(i=0;i<5;i++)
{
printf("enter the information of student number %d\n",i+1);
printf("Name:\n");
scanf("%s",st[i].name);
//getchar();//
//gets(s1[i].name);
printf("Age\n");
scanf("%d",&st[i].age);
printf("Marks\n");
scanf("%f",&st[i].marks);
}
}
int main()
{
printf("Read the information\n");
read(s1);
printf("enter the information\n");
display(s1);
}