From 2dfa546e78c04998d78c034f8b4011e645e9097d Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Sat, 20 Apr 2024 08:38:27 -0400 Subject: [PATCH] Something like ex15 extra credit --- lcthw/ex15.c | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/lcthw/ex15.c b/lcthw/ex15.c index 2a45c30..99734a8 100644 --- a/lcthw/ex15.c +++ b/lcthw/ex15.c @@ -13,8 +13,16 @@ int main(int argc, char *argv[]) int count = sizeof(ages) / sizeof(int); int i = 0; + char **first_name = &names[0]; + char **last_name = &names[count - 1]; + + printf("first_name=%p last_name=%p\n", first_name, last_name); + printf("*first_name=%s *last_name=%s\n", *first_name, *last_name); + + printf("---\n"); + // first way using indexing - for (i = 0; i < count; i++) { + for (i = count - 1; i > -1; i--) { printf("%s has %d years alive.\n", names[i], ages[i]); } @@ -25,14 +33,14 @@ int main(int argc, char *argv[]) char **cur_name = names; // second way using pointers - for (i = 0; i < count; i++) { + for (i = count - 1; i > -1; i--) { printf("%s is %d years old.\n", *(cur_name + i), *(cur_age + i)); } printf("---\n"); // third way, pointers are just arrays - for (i = 0; i < count; i++) { + for (i = count - 1; i > -1; i--) { printf("%s is %d years old again.\n", cur_name[i], cur_age[i]); }