Passing
Structure Members as arguments to Function
·
Individual
members of structure can pass to a function just like ordinary variables and also as composite.
The following program
demonstrates how to pass structure members as arguments to the function.
#include<stdio.h>
/*
structure is defined above all functions so it is global.
*/
struct student
{
char name[20];
int roll_no;
int marks;
};
void print_struct1(char name[], int roll_no, int marks);
void print_struct2(struct student temp);
int main()
{
struct student stu
= {"Tim", 1, 78};
print_struct1(stu.name, stu.roll_no,
stu.marks);
print_struct2(stu);
return 0;
}
void print_struct1(char name[], int roll_no, int marks)
{
printf("Name:
%s\n", name);
printf("Roll no:
%d\n", roll_no);
printf("Marks:
%d\n", marks);
printf("\n");
}
void print_struct2(struct student temp)
{
printf("Name:
%s\n", temp.name);
printf("Roll no: %d\n", temp.roll_no);
printf("Marks:
%d\n", temp.marks);
printf("\n");
}