Something like ex15 extra credit
This commit is contained in:
parent
224300e1cf
commit
2dfa546e78
14
lcthw/ex15.c
14
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]);
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user