using separate funcs for each way of looping/printing
This commit is contained in:
parent
0a0fc453c5
commit
bf6752edd6
46
ex15.c
46
ex15.c
@ -1,15 +1,7 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
int main(int argc, char *argv[])
|
void print_with_array_indexing(int count, char *names[], int ages[])
|
||||||
{
|
{
|
||||||
// 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;
|
int i = 0;
|
||||||
|
|
||||||
// first way using indexing
|
// first way using indexing
|
||||||
@ -19,7 +11,11 @@ int main(int argc, char *argv[])
|
|||||||
}
|
}
|
||||||
|
|
||||||
printf("---\n");
|
printf("---\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
void print_with_pointer_arithmetic(int count, char *names[], int ages[])
|
||||||
|
{
|
||||||
|
int i;
|
||||||
// setup the pointers to the start of the arrays
|
// setup the pointers to the start of the arrays
|
||||||
int *cur_age = ages;
|
int *cur_age = ages;
|
||||||
char **cur_name = names;
|
char **cur_name = names;
|
||||||
@ -31,7 +27,13 @@ int main(int argc, char *argv[])
|
|||||||
}
|
}
|
||||||
|
|
||||||
printf("---\n");
|
printf("---\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
void print_with_pointers_as_arrays(int count, char *names[], int ages[])
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
int *cur_age = ages;
|
||||||
|
char **cur_name = names;
|
||||||
// third way, pointers are just arrays
|
// third way, pointers are just arrays
|
||||||
for(i = 0; i < count; i++) {
|
for(i = 0; i < count; i++) {
|
||||||
printf("%s is %d years old again.\n",
|
printf("%s is %d years old again.\n",
|
||||||
@ -40,6 +42,13 @@ int main(int argc, char *argv[])
|
|||||||
|
|
||||||
printf("---\n");
|
printf("---\n");
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void print_in_stupidly_complex_way(int count, char *names[], int ages[])
|
||||||
|
{
|
||||||
|
int *cur_age = ages;
|
||||||
|
char **cur_name = names;
|
||||||
|
|
||||||
// fourth way with pointers in a stupid complex way
|
// fourth way with pointers in a stupid complex way
|
||||||
for(cur_name = names, cur_age = ages;
|
for(cur_name = names, cur_age = ages;
|
||||||
(cur_age - ages) < count;
|
(cur_age - ages) < count;
|
||||||
@ -50,7 +59,26 @@ int main(int argc, char *argv[])
|
|||||||
}
|
}
|
||||||
|
|
||||||
printf("---\n");
|
printf("---\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
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);
|
||||||
|
|
||||||
|
print_with_array_indexing(count, names, ages);
|
||||||
|
print_with_pointer_arithmetic(count, names, ages);
|
||||||
|
print_with_pointers_as_arrays(count, names, ages);
|
||||||
|
print_in_stupidly_complex_way(count, names, ages);
|
||||||
|
|
||||||
|
int i;
|
||||||
char **arg = argv;
|
char **arg = argv;
|
||||||
for(i = 0; i < argc; i++) {
|
for(i = 0; i < argc; i++) {
|
||||||
printf("argument %d is '%s' (address = %p)\n", i, *arg, arg);
|
printf("argument %d is '%s' (address = %p)\n", i, *arg, arg);
|
||||||
|
Loading…
Reference in New Issue
Block a user