-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathex16.c
94 lines (68 loc) · 1.89 KB
/
ex16.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
89
90
91
92
93
94
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Person {
char* name;
int height;
int weight;
int age;
};
struct Person create_person_on_stack(char* name, int age, int height, int weight) {
struct Person who;
who.name = name;
who.age = age;
who.height = height;
who.weight = weight;
return who;
}
void print_person_from_stack(struct Person who) {
printf("Name: %s\n", who.name);
printf("Age: %d\n", who.age);
printf("Height: %d\n", who.height);
printf("Weight: %d\n\n", who.weight);
}
struct Person* create_person(char* name, int age, int height, int weight) {
struct Person* who = malloc(sizeof(struct Person));
assert(who != NULL);
who->name = strdup(name);
who->age = age;
who->height = height;
who->weight = weight;
return who;
}
void destroy_person(struct Person* who) {
assert(who != NULL);
free(who->name);
free(who);
}
void print_person(struct Person* who) {
printf("Name: %s\n", who->name);
printf("Age: %d\n", who->age);
printf("Height: %d\n", who->height);
printf("Weight: %d\n\n", who->weight);
}
int main(int argc, char* argv[]) {
struct Person* joe = create_person("Joe Alex", 21, 180, 80);
struct Person* frank = create_person("Frank Viola", 45, 178, 90);
struct Person jonas = create_person_on_stack("Jonas Erhart", 21, 181, 70);
printf("Joe is at memory location %p.\n", joe);
printf("size of joe is %lu\n", sizeof(*joe));
print_person(joe);
printf("Frank is at memory location %p.\n", frank);
printf("size of frank is %lu\n", sizeof(*frank));
print_person(frank);
printf("Jonas is at memory location %p.\n", &jonas);
printf("size of jonas is %lu\n", sizeof(jonas));
print_person_from_stack(jonas);
joe->age += 20;
frank->age += 10;
jonas.age += 1;
print_person(joe);
print_person(frank);
print_person_from_stack(jonas);
destroy_person(joe);
destroy_person(frank);
// no need to free jonas
return 0;
}