-
Notifications
You must be signed in to change notification settings - Fork 0
/
lab3_2b.c
88 lines (67 loc) · 2.1 KB
/
lab3_2b.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/*
@Author: Arshad Shah
@Date: Monday, October 19, 2020 10:38 AM
@Email: [email protected]
@Filename: lab3_2b.c
@Last modified by: Arshad Shah
@Last modified time: Monday, October 19, 2020 10:38 AM
*/
/* A program to illustrate passing structure values to a function.
The program reads data for a student and displays it */
#include <stdio.h>
//global structure template
struct student_rec
{
int number ;
char name[26] ;
int age ;
int scores[5] ;
};
//Function prototype
void display_student_data(struct student_rec) ;
void get_student_data(struct student_rec *) ;
//main function
void main()
{
//declare variables
struct student_rec student ;
struct student_rec *student_ptr ;
student_ptr = &student ;
/* use a pointer to a structure variable as an argument */
get_student_data( student_ptr ) ;
/* use a structure variable as an argument */
display_student_data( student ) ;
}//end of main
/* Function: display_student_data() - displays student data
Arguments: the student structure */
void display_student_data (struct student_rec stud)
{
int i;
printf( "\n\nThe data in the student structure is:" ) ;
printf( "\nnumber is %d" , stud.number ) ;
printf( "\nname is %s" , stud.name ) ;
printf( "\nage is %d" , stud.age ) ;
printf( "\nscores are: " ) ;
for ( i= 0 ; i < 5 ; i++ )
printf( " %d " , stud.scores[i] ) ;
printf( "\n" ) ;
}//end of function display_student_data
/* Function: get_student_data() - This function reads student data
Arguments: a pointer to the student structure */
void get_student_data( struct student_rec *ptr )
{
int i ;
printf( "Student Number: " ) ;
scanf( "%d" , &(ptr->number) ) ;
printf( "Student Name: " ) ;
scanf( "%25s" , ptr->name ) ;
printf( "Age: " ) ;
scanf( "%d" , &(ptr->age) ) ;
//take input for test scores
printf( "5 Test Scores: " ) ;
//for loop to take input for the test score
for ( i= 0 ; i < 5 ; i++ )
{
scanf( "%d" , &(ptr->scores[i]) ) ;
}//end of for loop
}//end of function get_student_data