From 869f273723e3af7e1c133708152a95b5e3e9c6a3 Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Sun, 28 Apr 2024 22:10:16 -0400 Subject: [PATCH] Do the lcthw ex16 extra credit with stack structs --- lcthw/ex16ec.c | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 lcthw/ex16ec.c diff --git a/lcthw/ex16ec.c b/lcthw/ex16ec.c new file mode 100644 index 0000000..5f2bab9 --- /dev/null +++ b/lcthw/ex16ec.c @@ -0,0 +1,57 @@ +#include +#include +#include + +struct Person { + char *name; + int age; + int height; + int weight; +}; + +struct Person Person_create(char *name, int age, int height, int weight) +{ + struct Person who; + + who.name = strdup(name); + who.age = age; + who.height = height; + who.weight = weight; + + return who; +} + +void Person_print(struct Person who) +{ + printf("Name: %s\n", who.name); + printf("\tAge: %d\n", who.age); + printf("\tHeight: %d\n", who.height); + printf("\tWeight: %d\n", who.weight); +} + +int main(int argc, char *argv[]) +{ + // make two people structures + struct Person joe = Person_create("Joe Alex", 32, 64, 100); + + struct Person frank = Person_create("Frank Blank", 20, 72, 180); + + // print them out and where they are in memory + printf("Joe is at memory location %p:\n", &joe); + Person_print(joe); + + printf("Frank is at memory location %p:\n", &frank); + Person_print(frank); + + // make everyone age 20 years and print them again + joe.age += 20; + joe.height -= 2; + joe.weight += 40; + Person_print(joe); + + frank.age += 20; + frank.weight += 20; + Person_print(frank); + + return 0; +}