-
Notifications
You must be signed in to change notification settings - Fork 0
/
structure_functions.c
63 lines (52 loc) · 1.63 KB
/
structure_functions.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
/* A program to illustrate passing structure values to a function.
The program reads data for a student and displays it */
#include <stdio.h>
struct student_rec /* global structure template */
{
int number ;
char name[26] ;
int age ;
int scores[5] ;
};
// prototype
void display_student_data(struct student_rec) ;
void get_student_data(struct student_rec *) ;
void main()
{
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 ) ;
}
/* 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" ) ;
}
/* 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) ) ;
printf( "5 Test Scores: " ) ;
for ( i= 0 ; i < 5 ; i++ )
scanf( "%d" , &(ptr->scores[i]) ) ;
}