Something like ex15 extra credit

This commit is contained in:
Dan Buch 2024-04-20 08:38:27 -04:00
parent 224300e1cf
commit 2dfa546e78
Signed by: meatballhat
GPG Key ID: A12F782281063434

View File

@ -13,8 +13,16 @@ int main(int argc, char *argv[])
int count = sizeof(ages) / sizeof(int); int count = sizeof(ages) / sizeof(int);
int i = 0; 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 // 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]); 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; char **cur_name = names;
// second way using pointers // 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("%s is %d years old.\n", *(cur_name + i), *(cur_age + i));
} }
printf("---\n"); printf("---\n");
// third way, pointers are just arrays // 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]); printf("%s is %d years old again.\n", cur_name[i], cur_age[i]);
} }