-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathraise.c
More file actions
42 lines (33 loc) · 986 Bytes
/
raise.c
File metadata and controls
42 lines (33 loc) · 986 Bytes
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
/*
* raise.c
* demonstrates the use of passing a struct to a function by reference
*/
#include <stdio.h>
// make a person datatype using typedef
typedef struct {
char* name;
float salary;
} person;
void giveRaise(person *p, float percent)
{
// to access a member of a struct passed by reference we need to use ->
printf("Give a %.2f%% raise to %s\n", percent*100, p->name);
p->salary = (percent*p->salary)+p->salary;
}
int main(void)
{
printf("Enter Paul's salary: ");
float salary;
scanf("%f", &salary);
// make a person called paul
person paul = { "Paul Osborn", salary};
printf("%s: $%.2f\n", paul.name, paul.salary);
// ask the user how much of a raise to give
int percent;
printf("Enter a percentage to raise: ");
scanf("%d", &percent);
// apply the raise
giveRaise(&paul, percent/100.0); // pass the person by reference
printf("%s: $%.2f\n", paul.name, paul.salary);
return 0;
}