Pasing elements of a struct variable as parameters to a function.

#include <stdio.h>

typedef struct student
{
int rollno;
char name[20];
float percent;
}Student;

void getStudent(int *, char *, float *);
void showStudent(int, const char * , float);

int main(void)
{
Student s;

printf("\nEnter student info : ");
getStudent(&s.rollno, s.name, &s.percent); /* pass by reference */

printf("\nDisplaying student info : ");
showStudent(s.rollno, s.name, s.percent); /* pass by value and reference */

return 0;
}

void getStudent(int *rp, char *nm, float *pp)
{
printf( "\nEnter Roll no. : " );
scanf( "%d", rp );

printf( "Name : " );
fflush(stdin);
gets(nm);

printf( "Percentage : " );
scanf( "%f", pp );
}

void showStudent(int rollno, const char *nm, float per)
{
printf( "\nRoll No\t\t: %d", rollno );
printf( "\nName\t\t: %s", nm );
printf( "\nPercentage\t: %f", per );
}

No comments:

Post a Comment

kiss on google ads if you are anonymous because your ip is trackable.thank you.

......from.admin