From 8c6e140dad5eb3e97832db89515e0758ffa9e4f4 Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Sun, 18 Sep 2011 13:34:09 -0400 Subject: [PATCH] adding in comments so that line numbers match up (which I should have been doing all along) --- ex15.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/ex15.c b/ex15.c index c890f92..1853556 100644 --- a/ex15.c +++ b/ex15.c @@ -2,15 +2,17 @@ int main(int argc, char *argv[]) { + // create two arrays we care about int ages[] = {23, 43, 12, 89, 2}; char *names[] = { "Alan", "Frank", "Mary", "John", "Lisa" }; - + // safely get the size of ages int count = sizeof(ages) / sizeof(int); int i = 0; + // first way using indexing for(i = 0; i < count; i++) { printf("%s has %d years alive.\n", names[i], ages[i]); @@ -18,9 +20,11 @@ int main(int argc, char *argv[]) printf("---\n"); + // setup the pointers to the start of the arrays int *cur_age = ages; char **cur_name = names; + // second way using pointers for(i = 0; i < count; i++) { printf("%s is %d years old.\n", *(cur_name+i), *(cur_age+i)); @@ -28,6 +32,7 @@ int main(int argc, char *argv[]) printf("---\n"); + // third way, pointers are just arrays for(i = 0; i < count; i++) { printf("%s is %d years old again.\n", cur_name[i], cur_age[i]); @@ -35,6 +40,7 @@ int main(int argc, char *argv[]) printf("---\n"); + // fourth way with pointers in a stupid complex way for(cur_name = names, cur_age = ages; (cur_age - ages) < count; cur_name++, cur_age++)